dolibarr 21.0.0-alpha
ProductCombination2ValuePair.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
3 * Copyright (C) 2022 Open-Dsi <support@open-dsi.fr>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.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
43{
48 private $db;
49
54 public $id;
55
61 public $fk_prod_combination;
62
68 public $fk_prod_attr;
69
75 public $fk_prod_attr_val;
76
81 public $error;
82
87 public $errors = array();
88
94 public function __construct(DoliDB $db)
95 {
96 $this->db = $db;
97 }
98
104 public function __toString()
105 {
106 require_once DOL_DOCUMENT_ROOT . '/variants/class/ProductAttributeValue.class.php';
107 require_once DOL_DOCUMENT_ROOT . '/variants/class/ProductAttribute.class.php';
108
109 $prodattr = new ProductAttribute($this->db);
110 $prodattrval = new ProductAttributeValue($this->db);
111
112 $prodattr->fetch($this->fk_prod_attr);
113 $prodattrval->fetch($this->fk_prod_attr_val);
114
115 return $prodattr->label . ': ' . $prodattrval->value;
116 }
117
124 public function create($user)
125 {
126 $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_attribute_combination2val
127 (fk_prod_combination, fk_prod_attr, fk_prod_attr_val)
128 VALUES(" . (int) $this->fk_prod_combination . ", " . (int) $this->fk_prod_attr . ", " . (int) $this->fk_prod_attr_val . ")";
129
130 $query = $this->db->query($sql);
131
132 if ($query) {
133 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'product_attribute_combination2val');
134
135 return 1;
136 }
137
138 return -1;
139 }
140
147 public function fetchByFkCombination($fk_combination)
148 {
149 $sql = "SELECT
150 c.rowid,
151 c2v.fk_prod_attr_val,
152 c2v.fk_prod_attr,
153 c2v.fk_prod_combination
154 FROM " . MAIN_DB_PREFIX . "product_attribute c LEFT JOIN " . MAIN_DB_PREFIX . "product_attribute_combination2val c2v ON c.rowid = c2v.fk_prod_attr
155 WHERE c2v.fk_prod_combination = " . (int) $fk_combination;
156
157 $sql .= $this->db->order('c.position', 'asc');
158
159 $query = $this->db->query($sql);
160
161 if (!$query) {
162 return -1;
163 }
164
165 $return = array();
166
167 while ($obj = $this->db->fetch_object($query)) {
168 $tmp = new ProductCombination2ValuePair($this->db);
169 $tmp->fk_prod_attr_val = $obj->fk_prod_attr_val;
170 $tmp->fk_prod_attr = $obj->fk_prod_attr;
171 $tmp->fk_prod_combination = $obj->fk_prod_combination;
172 $tmp->id = $obj->rowid;
173
174 $return[] = $tmp;
175 }
176
177 return $return;
178 }
179
186 public function deleteByFkCombination($fk_combination)
187 {
188 $sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_attribute_combination2val WHERE fk_prod_combination = " . (int) $fk_combination;
189
190 if ($this->db->query($sql)) {
191 return 1;
192 }
193
194 return -1;
195 }
196}
Class to manage Dolibarr database access.
Class ProductAttribute Used to represent a Product attribute Examples:
Class ProductAttributeValue Used to represent a product attribute value.
Class ProductCombination2ValuePair Used to represent the relation between a variant and its attribute...
deleteByFkCombination($fk_combination)
Delete all ProductCombination2ValuePair linked to a given ProductCombination ID.
fetchByFkCombination($fk_combination)
Retrieve all ProductCombination2ValuePair linked to a given ProductCombination ID.
create($user)
Create a ProductCombination2ValuePair.
__toString()
Translates this class to a human-readable string.