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