dolibarr 19.0.3
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
58 public $error;
59
63 public $errors = array();
64
70 public function __construct(DoliDB $db)
71 {
72 $this->db = $db;
73 }
74
80 public function __toString()
81 {
82 require_once DOL_DOCUMENT_ROOT . '/variants/class/ProductAttributeValue.class.php';
83 require_once DOL_DOCUMENT_ROOT . '/variants/class/ProductAttribute.class.php';
84
85 $prodattr = new ProductAttribute($this->db);
86 $prodattrval = new ProductAttributeValue($this->db);
87
88 $prodattr->fetch($this->fk_prod_attr);
89 $prodattrval->fetch($this->fk_prod_attr_val);
90
91 return $prodattr->label . ': ' . $prodattrval->value;
92 }
93
100 public function create($user)
101 {
102 $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_attribute_combination2val
103 (fk_prod_combination, fk_prod_attr, fk_prod_attr_val)
104 VALUES(" . (int) $this->fk_prod_combination . ", " . (int) $this->fk_prod_attr . ", " . (int) $this->fk_prod_attr_val . ")";
105
106 $query = $this->db->query($sql);
107
108 if ($query) {
109 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'product_attribute_combination2val');
110
111 return 1;
112 }
113
114 return -1;
115 }
116
123 public function fetchByFkCombination($fk_combination)
124 {
125 $sql = "SELECT
126 c.rowid,
127 c2v.fk_prod_attr_val,
128 c2v.fk_prod_attr,
129 c2v.fk_prod_combination
130 FROM " . MAIN_DB_PREFIX . "product_attribute c LEFT JOIN " . MAIN_DB_PREFIX . "product_attribute_combination2val c2v ON c.rowid = c2v.fk_prod_attr
131 WHERE c2v.fk_prod_combination = " . (int) $fk_combination;
132
133 $sql .= $this->db->order('c.position', 'asc');
134
135 $query = $this->db->query($sql);
136
137 if (!$query) {
138 return -1;
139 }
140
141 $return = array();
142
143 while ($obj = $this->db->fetch_object($query)) {
144 $tmp = new ProductCombination2ValuePair($this->db);
145 $tmp->fk_prod_attr_val = $obj->fk_prod_attr_val;
146 $tmp->fk_prod_attr = $obj->fk_prod_attr;
147 $tmp->fk_prod_combination = $obj->fk_prod_combination;
148 $tmp->id = $obj->rowid;
149
150 $return[] = $tmp;
151 }
152
153 return $return;
154 }
155
162 public function deleteByFkCombination($fk_combination)
163 {
164 $sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_attribute_combination2val WHERE fk_prod_combination = " . (int) $fk_combination;
165
166 if ($this->db->query($sql)) {
167 return 1;
168 }
169
170 return -1;
171 }
172}
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.