1
I’m using the ORM native of Cakephp 3 to retrieve the product table records (through the ID) along with the records that references it with the following method:
public function view($id)
{
if($this->request->is('get'))
{
$product = $this->Products->get($id, [
'contain' => ['Stores', 'Bookings', 'ProductFeatures', 'ProductMedias']
]);
$this->set('product', $product);
$bannerType = 2;
$bannersQuantity = 1;
$fullBanners = $this->Search->listAllBanners($bannerType, $bannersQuantity);
$this->set('fullBanners', $fullBanners);
$logged = $this->Auth->user();
$this->set('logged', $logged);
$this->set('pageTitle', $product['product_name'].' - Stores');
}
}
It is being returned basically all the records I need but how do I return each record of the referenced Features table in the product_features table
Bank tables:
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
store_id INT NOT NULL,
sub_category_id INT NOT NULL,
quantity INT NOT NULL,
sold INT NOT NULL,
description VARCHAR(1000),
price DECIMAL(7,2) NOT NULL,
old_price DECIMAL(7,2) NOT NULL,
visited INT NOT NULL,
thumbnail VARCHAR(255) NOT NULL,
status INT NOT NULL,
created DATETIME,
modified DATETIME,
FOREIGN KEY store_key (store_id) REFERENCES stores(id),
FOREIGN KEY sub_category_key (sub_category_id) REFERENCES sub_categories(id)
);
CREATE TABLE sub_categories (
id INT AUTO_INCREMENT PRIMARY KEY,
sub_category_name VARCHAR(255) NOT NULL,
category_id INT NOT NULL,
created DATETIME,
modified DATETIME,
FOREIGN KEY category_key (category_id) REFERENCES categories(id)
);
CREATE TABLE features (
id INT AUTO_INCREMENT PRIMARY KEY,
feature_name VARCHAR(255) NOT NULL,
created DATETIME,
modified DATETIME
);
CREATE TABLE product_features (
id INT AUTO_INCREMENT PRIMARY KEY,
feature_value VARCHAR(255) NOT NULL,
feature_id INT NOT NULL,
product_id INT NOT NULL,
created DATETIME,
modified DATETIME,
FOREIGN KEY feature_key (feature_id) REFERENCES features(id),
FOREIGN KEY product_key (product_id) REFERENCES products(id)
);
var_dump result($product)
object(App\Model\Entity\Product)#249 (25) {
["id"]=>
int(1)
["product_name"]=>
string(6) "Cama X"
["store_id"]=>
int(1)
["quantity"]=>
int(20)
["description"]=>
string(9) "Muito Boa"
["price"]=>
float(550.5)
["status"]=>
int(1)
["created"]=>
object(Cake\I18n\Time)#244 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["modified"]=>
object(Cake\I18n\Time)#247 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["sub_category_id"]=>
int(8)
["sold"]=>
int(40)
["old_price"]=>
float(400)
["visited"]=>
int(777)
["thumbnail"]=>
string(21) "products/product2.jpg"
["product_medias"]=>
array(2) {
[0]=>
object(App\Model\Entity\ProductMedia)#239 (12) {
["id"]=>
int(1)
["product_id"]=>
int(1)
["media_id"]=>
int(1)
["created"]=>
object(Cake\I18n\Time)#232 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["modified"]=>
object(Cake\I18n\Time)#235 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["[new]"]=>
bool(false)
["[accessible]"]=>
array(4) {
["product_id"]=>
bool(true)
["media_id"]=>
bool(true)
["product"]=>
bool(true)
["media"]=>
bool(true)
}
["[dirty]"]=>
array(0) {
}
["[original]"]=>
array(0) {
}
["[virtual]"]=>
array(0) {
}
["[errors]"]=>
array(0) {
}
["[repository]"]=>
string(13) "ProductMedias"
}
[1]=>
object(App\Model\Entity\ProductMedia)#237 (12) {
["id"]=>
int(8)
["product_id"]=>
int(1)
["media_id"]=>
int(8)
["created"]=>
object(Cake\I18n\Time)#240 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["modified"]=>
object(Cake\I18n\Time)#231 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["[new]"]=>
bool(false)
["[accessible]"]=>
array(4) {
["product_id"]=>
bool(true)
["media_id"]=>
bool(true)
["product"]=>
bool(true)
["media"]=>
bool(true)
}
["[dirty]"]=>
array(0) {
}
["[original]"]=>
array(0) {
}
["[virtual]"]=>
array(0) {
}
["[errors]"]=>
array(0) {
}
["[repository]"]=>
string(13) "ProductMedias"
}
}
["product_features"]=>
array(2) {
[0]=>
object(App\Model\Entity\ProductFeature)#218 (13) {
["id"]=>
int(1)
["feature_value"]=>
string(5) "30 Cm"
["feature_id"]=>
int(1)
["product_id"]=>
int(1)
["created"]=>
object(Cake\I18n\Time)#211 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["modified"]=>
object(Cake\I18n\Time)#214 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["[new]"]=>
bool(false)
["[accessible]"]=>
array(5) {
["feature_value"]=>
bool(true)
["feature_id"]=>
bool(true)
["product_id"]=>
bool(true)
["feature"]=>
bool(true)
["product"]=>
bool(true)
}
["[dirty]"]=>
array(0) {
}
["[original]"]=>
array(0) {
}
["[virtual]"]=>
array(0) {
}
["[errors]"]=>
array(0) {
}
["[repository]"]=>
string(15) "ProductFeatures"
}
[1]=>
object(App\Model\Entity\ProductFeature)#216 (13) {
["id"]=>
int(8)
["feature_value"]=>
string(5) "20 Kg"
["feature_id"]=>
int(2)
["product_id"]=>
int(1)
["created"]=>
object(Cake\I18n\Time)#219 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["modified"]=>
object(Cake\I18n\Time)#210 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["[new]"]=>
bool(false)
["[accessible]"]=>
array(5) {
["feature_value"]=>
bool(true)
["feature_id"]=>
bool(true)
["product_id"]=>
bool(true)
["feature"]=>
bool(true)
["product"]=>
bool(true)
}
["[dirty]"]=>
array(0) {
}
["[original]"]=>
array(0) {
}
["[virtual]"]=>
array(0) {
}
["[errors]"]=>
array(0) {
}
["[repository]"]=>
string(15) "ProductFeatures"
}
}
["bookings"]=>
array(1) {
[0]=>
object(App\Model\Entity\Booking)#196 (13) {
["id"]=>
int(1)
["product_id"]=>
int(1)
["quantity"]=>
int(5)
["user_id"]=>
int(1)
["created"]=>
object(Cake\I18n\Time)#194 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["modified"]=>
object(Cake\I18n\Time)#195 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["[new]"]=>
bool(false)
["[accessible]"]=>
array(5) {
["product_id"]=>
bool(true)
["quantity"]=>
bool(true)
["user_id"]=>
bool(true)
["product"]=>
bool(true)
["user"]=>
bool(true)
}
["[dirty]"]=>
array(0) {
}
["[original]"]=>
array(0) {
}
["[virtual]"]=>
array(0) {
}
["[errors]"]=>
array(0) {
}
["[repository]"]=>
string(8) "Bookings"
}
}
["store"]=>
object(App\Model\Entity\Store)#243 (12) {
["id"]=>
int(1)
["store_name"]=>
string(6) "Loja A"
["user_id"]=>
int(1)
["created"]=>
object(Cake\I18n\Time)#251 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["modified"]=>
object(Cake\I18n\Time)#252 (3) {
["time"]=>
string(24) "2015-07-17T00:00:00+0000"
["timezone"]=>
string(3) "UTC"
["fixedNowTime"]=>
bool(false)
}
["[new]"]=>
bool(false)
["[accessible]"]=>
array(4) {
["store_name"]=>
bool(true)
["user_id"]=>
bool(true)
["user"]=>
bool(true)
["products"]=>
bool(true)
}
["[dirty]"]=>
array(0) {
}
["[original]"]=>
array(0) {
}
["[virtual]"]=>
array(0) {
}
["[errors]"]=>
array(0) {
}
["[repository]"]=>
string(6) "Stores"
}
["[new]"]=>
bool(false)
["[accessible]"]=>
array(10) {
["product_name"]=>
bool(true)
["store_id"]=>
bool(true)
["quantity"]=>
bool(true)
["description"]=>
bool(true)
["price"]=>
bool(true)
["status"]=>
bool(true)
["store"]=>
bool(true)
["bookings"]=>
bool(true)
["product_features"]=>
bool(true)
["product_medias"]=>
bool(true)
}
["[dirty]"]=>
array(0) {
}
["[original]"]=>
array(0) {
}
["[virtual]"]=>
array(0) {
}
["[errors]"]=>
array(0) {
}
["[repository]"]=>
string(8) "Products"
}
@Erloncharles this
$this->Products->get()
vei by default with the file generation bybake
– Ricardo
He was looking for recursive in the documentation of 3, how would find it without him then?
– Ricardo
Post the result of your
$this->Products->get()
for me to analyze, because according to the documentation was to return yourproduct_features
also. but if you want you can try this find$this->Products->find('first', [
 'conditions' => ['Products.id' => $id],
 'contain' => ['Stores', 'Bookings', 'ProductFeatures', 'ProductMedias']
]);
– Erlon Charles
$this->Products->get() parameter missing error
– Ricardo
@Erloncharles did not work the second approach, I had to modify because the 'first' is no longer supported, I had to make a
->first()
– Ricardo
I get it, I’m still confused between the cake2 and cake3 stuff, but put what’s in yours
$product
original.– Erlon Charles
@Erloncharles did a var_dump on the result my first query and the result was huge is there any way to post it off the site (because of the size)? I will try to reduce my question to insert it
– Ricardo
@Erloncharles added var_dump result
– Ricardo
Let’s go continue this discussion in chat.
– Erlon Charles