dolibarr 20.0.0
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 *
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
42{
47 private $db;
48
53 public $id;
54
60 public $fk_prod_combination;
61
67 public $fk_prod_attr;
68
74 public $fk_prod_attr_val;
75
80 public $error;
81
86 public $errors = array();
87
93 public function __construct(DoliDB $db)
94 {
95 $this->db = $db;
96 }
97
103 public function __toString()
104 {
105 require_once DOL_DOCUMENT_ROOT . '/variants/class/ProductAttributeValue.class.php';
106 require_once DOL_DOCUMENT_ROOT . '/variants/class/ProductAttribute.class.php';
107
108 $prodattr = new ProductAttribute($this->db);
109 $prodattrval = new ProductAttributeValue($this->db);
110
111 $prodattr->fetch($this->fk_prod_attr);
112 $prodattrval->fetch($this->fk_prod_attr_val);
113
114 return $prodattr->label . ': ' . $prodattrval->value;
115 }
116
123 public function create($user)
124 {
125 $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_attribute_combination2val
126 (fk_prod_combination, fk_prod_attr, fk_prod_attr_val)
127 VALUES(" . (int) $this->fk_prod_combination . ", " . (int) $this->fk_prod_attr . ", " . (int) $this->fk_prod_attr_val . ")";
128
129 $query = $this->db->query($sql);
130
131 if ($query) {
132 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'product_attribute_combination2val');
133
134 return 1;
135 }
136
137 return -1;
138 }
139
146 public function fetchByFkCombination($fk_combination)
147 {
148 $sql = "SELECT
149 c.rowid,
150 c2v.fk_prod_attr_val,
151 c2v.fk_prod_attr,
152 c2v.fk_prod_combination
153 FROM " . MAIN_DB_PREFIX . "product_attribute c LEFT JOIN " . MAIN_DB_PREFIX . "product_attribute_combination2val c2v ON c.rowid = c2v.fk_prod_attr
154 WHERE c2v.fk_prod_combination = " . (int) $fk_combination;
155
156 $sql .= $this->db->order('c.position', 'asc');
157
158 $query = $this->db->query($sql);
159
160 if (!$query) {
161 return -1;
162 }
163
164 $return = array();
165
166 while ($obj = $this->db->fetch_object($query)) {
167 $tmp = new ProductCombination2ValuePair($this->db);
168 $tmp->fk_prod_attr_val = $obj->fk_prod_attr_val;
169 $tmp->fk_prod_attr = $obj->fk_prod_attr;
170 $tmp->fk_prod_combination = $obj->fk_prod_combination;
171 $tmp->id = $obj->rowid;
172
173 $return[] = $tmp;
174 }
175
176 return $return;
177 }
178
185 public function deleteByFkCombination($fk_combination)
186 {
187 $sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_attribute_combination2val WHERE fk_prod_combination = " . (int) $fk_combination;
188
189 if ($this->db->query($sql)) {
190 return 1;
191 }
192
193 return -1;
194 }
195}
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.