Skip to content

Instantly share code, notes, and snippets.

@markstory
Created December 16, 2015 03:35
Show Gist options
  • Save markstory/e554cdbce5c98b5b61f9 to your computer and use it in GitHub Desktop.
Save markstory/e554cdbce5c98b5b61f9 to your computer and use it in GitHub Desktop.
>>> $products = \Cake\ORM\TableRegistry::get('Products');
=> App\Model\Table\ProductsTable {#214
+"registryAlias": "Products",
+"table": "DAS_Product",
+"alias": "Products",
+"entityClass": "\Cake\ORM\Entity",
+"associations": [
"opportunities",
],
+"behaviors": [],
+"defaultConnection": "default",
+"connectionName": "default",
}
>>> $products->find()->where(['id'=>1])->contain(['Opportunities'])->toArray();
2015-12-16 03:29:19 Debug: SELECT `Products`.`id` AS `Products__id`, `Products`.`name` AS `Products__name`, `Products`.`is_active` AS `Products__is_active`, `Products`.`deleted` AS `Products__deleted`, `Products`.`deleted_by` AS `Products__deleted_by`, `Products`.`deleted_on` AS `Products__deleted_on`, `Products`.`parent_id` AS `Products__parent_id` FROM `DAS_Product` `Products` WHERE `id` = 1
2015-12-16 03:29:19 Debug: SHOW FULL COLUMNS FROM `DAS_OpportunityProduct`
2015-12-16 03:29:19 Debug: SHOW INDEXES FROM `DAS_OpportunityProduct`
2015-12-16 03:29:19 Debug: SELECT * FROM information_schema.key_column_usage AS kcu
INNER JOIN information_schema.referential_constraints AS rc
ON (
kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
AND kcu.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA
)
WHERE kcu.TABLE_SCHEMA = 'bookmarker' AND kcu.TABLE_NAME = 'DAS_OpportunityProduct' AND rc.TABLE_NAME = 'DAS_OpportunityProduct'
2015-12-16 03:29:19 Debug: SHOW TABLE STATUS WHERE Name = 'DAS_OpportunityProduct'
2015-12-16 03:29:19 Debug: SELECT `OpportunityProducts`.`DAS_Opportunity_id` AS `OpportunityProducts__DAS_Opportunity_id`, `OpportunityProducts`.`id` AS `OpportunityProducts__id`, `OpportunityProducts`.`DAS_Product_id` AS `OpportunityProducts__DAS_Product_id`, `OpportunityProducts`.`ts_createDate` AS `OpportunityProducts__ts_createDate`, `OpportunityProducts`.`createdBy_user_id` AS `OpportunityProducts__createdBy_user_id`, `OpportunityProducts`.`is_deleted` AS `OpportunityProducts__is_deleted`, `OpportunityProducts`.`deletedBy_User_id` AS `OpportunityProducts__deletedBy_User_id`, `OpportunityProducts`.`deleteDateTime` AS `OpportunityProducts__deleteDateTime`, `Opportunities`.`id` AS `Opportunities__id`, `Opportunities`.`name` AS `Opportunities__name` FROM `DAS_Opportunity` `Opportunities` INNER JOIN `DAS_OpportunityProduct` `OpportunityProducts` ON `Opportunities`.`id` = (`OpportunityProducts`.`DAS_Opportunity_id`) INNER JOIN (SELECT (`Products`.`id`) FROM `DAS_Product` `Products` WHERE `id` = 1 GROUP BY `Products`.`id` ) `Products` ON `OpportunityProducts`.`DAS_Product_id` = (`Products`.`id`)
=> [
Cake\ORM\Entity {#326
+"id": 1,
+"name": "spork",
+"is_active": 1,
+"deleted": 0,
+"deleted_by": null,
+"deleted_on": null,
+"parent_id": null,
+"opportunities": [
Cake\ORM\Entity {#310
+"id": 1,
+"name": "First",
+"_joinData": Cake\ORM\Entity {#309
+"DAS_Opportunity_id": 1,
+"id": 1,
+"DAS_Product_id": 1,
+"ts_createDate": null,
+"createdBy_user_id": null,
+"is_deleted": null,
+"deletedBy_User_id": null,
+"deleteDateTime": null,
+"[new]": false,
+"[accessible]": [
"*" => true,
],
+"[dirty]": [],
+"[original]": [],
+"[virtual]": [],
+"[errors]": [],
+"[repository]": "OpportunityProducts",
},
+"[new]": false,
+"[accessible]": [
"*" => true,
],
+"[dirty]": [],
+"[original]": [],
+"[virtual]": [],
+"[errors]": [],
+"[repository]": "Opportunities",
},
Cake\ORM\Entity {#312
+"id": 2,
+"name": "Second",
+"_joinData": Cake\ORM\Entity {#311
+"DAS_Opportunity_id": 2,
+"id": 2,
+"DAS_Product_id": 1,
+"ts_createDate": null,
+"createdBy_user_id": null,
+"is_deleted": null,
+"deletedBy_User_id": null,
+"deleteDateTime": null,
+"[new]": false,
+"[accessible]": [
"*" => true,
],
+"[dirty]": [],
+"[original]": [],
+"[virtual]": [],
+"[errors]": [],
+"[repository]": "OpportunityProducts",
},
+"[new]": false,
+"[accessible]": [
"*" => true,
],
+"[dirty]": [],
+"[original]": [],
+"[virtual]": [],
+"[errors]": [],
+"[repository]": "Opportunities",
},
Cake\ORM\Entity {#314
+"id": 3,
+"name": "Third",
+"_joinData": Cake\ORM\Entity {#313
+"DAS_Opportunity_id": 3,
+"id": 3,
+"DAS_Product_id": 1,
+"ts_createDate": null,
+"createdBy_user_id": null,
+"is_deleted": null,
+"deletedBy_User_id": null,
+"deleteDateTime": null,
+"[new]": false,
+"[accessible]": [
"*" => true,
],
+"[dirty]": [],
+"[original]": [],
+"[virtual]": [],
+"[errors]": [],
+"[repository]": "OpportunityProducts",
},
+"[new]": false,
+"[accessible]": [
"*" => true,
],
+"[dirty]": [],
+"[original]": [],
+"[virtual]": [],
+"[errors]": [],
+"[repository]": "Opportunities",
},
],
+"[new]": false,
+"[accessible]": [
"*" => true,
],
+"[dirty]": [],
+"[original]": [],
+"[virtual]": [],
+"[errors]": [],
+"[repository]": "Products",
},
]
>>>
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class OpportunitiesTable extends Table {
public function initialize(array $config){
$this->table("DAS_Opportunity");
}
}
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class OpportunityProductsTable extends Table {
public function initialize(array $config){
$this->table("DAS_OpportunityProduct");
$this->primaryKey("id");
$this->belongsTo("Opportunities", [
'foreignKey' => 'DAS_Opportunity_id',
'bindingKey' => 'id'
]);
$this->belongsTo("Products", [
'foreignKey' => 'DAS_Product_id',
'bindingKey' => 'id'
]);
}
}
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class ProductsTable extends Table {
public function initialize(array $config){
$this->table("DAS_Product");
$this->belongsToMany('Opportunities', [
'through' => 'OpportunityProducts',
'className' => 'Opportunities',
'targetForeignKey' => 'DAS_Opportunity_id',
'foreignKey' => 'DAS_Product_id',
'strategy'=>'subquery',
]);
}
}
INSERT INTO DAS_Opportunity (name) values ('First'), ('Second'), ('Third');
insert into DAS_Product (name, is_active, deleted) values ('spork', true, false), ('spoon', true, false), ('knife', true, false);
insert into DAS_OpportunityProduct (DAS_Product_id, DAS_Opportunity_id) values (1, 1), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3,3);
CREATE TABLE `DAS_Product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) DEFAULT NULL,
`is_active` int(11) DEFAULT NULL,
`deleted` int(11) DEFAULT NULL,
`deleted_by` int(11) DEFAULT NULL,
`deleted_on` datetime DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `DAS_Opportunity` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `DAS_OpportunityProduct` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`DAS_Product_id` int(11) DEFAULT NULL,
`DAS_Opportunity_id` int(11) DEFAULT NULL,
`ts_createDate` datetime DEFAULT NULL,
`createdBy_user_id` int(11) DEFAULT NULL,
`is_deleted` tinyint(4) DEFAULT NULL,
`deletedBy_User_id` int(11) DEFAULT NULL,
`deleteDateTime` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment