dolibarr  17.0.4
ProductCombination2ValuePair.class.php
1 <?php
2 /* Copyright (C) 2016 Marcos GarcĂ­a <marcosgdf@gmail.com>
3  * Copyright (C) 2022 Open-Dsi <support@open-dsi.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
24 {
29  private $db;
30 
35  public $id;
36 
41  public $fk_prod_combination;
42 
47  public $fk_prod_attr;
48 
53  public $fk_prod_attr_val;
54 
60  public function __construct(DoliDB $db)
61  {
62  $this->db = $db;
63  }
64 
70  public function __toString()
71  {
72  require_once DOL_DOCUMENT_ROOT . '/variants/class/ProductAttributeValue.class.php';
73  require_once DOL_DOCUMENT_ROOT . '/variants/class/ProductAttribute.class.php';
74 
75  $prodattr = new ProductAttribute($this->db);
76  $prodattrval = new ProductAttributeValue($this->db);
77 
78  $prodattr->fetch($this->fk_prod_attr);
79  $prodattrval->fetch($this->fk_prod_attr_val);
80 
81  return $prodattr->label . ': ' . $prodattrval->value;
82  }
83 
90  public function create($user)
91  {
92  $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_attribute_combination2val
93  (fk_prod_combination, fk_prod_attr, fk_prod_attr_val)
94  VALUES(" . (int) $this->fk_prod_combination . ", " . (int) $this->fk_prod_attr . ", " . (int) $this->fk_prod_attr_val . ")";
95 
96  $query = $this->db->query($sql);
97 
98  if ($query) {
99  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'product_attribute_combination2val');
100 
101  return 1;
102  }
103 
104  return -1;
105  }
106 
113  public function fetchByFkCombination($fk_combination)
114  {
115  $sql = "SELECT
116  c.rowid,
117  c2v.fk_prod_attr_val,
118  c2v.fk_prod_attr,
119  c2v.fk_prod_combination
120  FROM " . MAIN_DB_PREFIX . "product_attribute c LEFT JOIN " . MAIN_DB_PREFIX . "product_attribute_combination2val c2v ON c.rowid = c2v.fk_prod_attr
121  WHERE c2v.fk_prod_combination = " . (int) $fk_combination;
122 
123  $sql .= $this->db->order('c.position', 'asc');
124 
125  $query = $this->db->query($sql);
126 
127  if (!$query) {
128  return -1;
129  }
130 
131  $return = array();
132 
133  while ($obj = $this->db->fetch_object($query)) {
134  $tmp = new ProductCombination2ValuePair($this->db);
135  $tmp->fk_prod_attr_val = $obj->fk_prod_attr_val;
136  $tmp->fk_prod_attr = $obj->fk_prod_attr;
137  $tmp->fk_prod_combination = $obj->fk_prod_combination;
138  $tmp->id = $obj->rowid;
139 
140  $return[] = $tmp;
141  }
142 
143  return $return;
144  }
145 
152  public function deleteByFkCombination($fk_combination)
153  {
154  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_attribute_combination2val WHERE fk_prod_combination = " . (int) $fk_combination;
155 
156  if ($this->db->query($sql)) {
157  return 1;
158  }
159 
160  return -1;
161  }
162 }
Class to manage Dolibarr database access.
Class ProductAttribute Used to represent a product attribute.
Class ProductAttributeValue Used to represent a product attribute value.
Class ProductCombination2ValuePair Used to represent the relation between a product combination,...
deleteByFkCombination($fk_combination)
Deletes a product combination 2 value pair.
fetchByFkCombination($fk_combination)
Retrieves a product combination 2 value pair from its rowid.
create($user)
Creates a product combination 2 value pair.
__toString()
Translates this class to a human-readable string.
$conf db
API class for accounts.
Definition: inc.php:41