Skip to content

Instantly share code, notes, and snippets.

@half2me
half2me / table.php
Created July 7, 2016 12:34
Inner JOIN bug in CakePHP
<?php
// So I'm going to be demonstrating a problem with INNER joins
//Bake automatically creates INNER JOINs for a belongsTo association,
// where the foreign key is not allowed to be null (Always BelongsTo)
// This is great if you have an (AlwaysBelongsTo) association, but
// when you don't always BelongTo another model, we need LEFT joins.
// The problem arised when you have a model that someTimesBelongsTo another model
// and that other model alwaysBelongsTo another model (LEFT, then INNER joins)
// Cake doesn't use proper parenthesis in these cases, but simply lines the joins up,
// one after another.
<?php
namespace App\Test\Fixture;
use Cake\TestSuite\Fixture\TestFixture;
/**
* ReservationsFixture
*
*/
class ReservationsFixture extends TestFixture
@half2me
half2me / log.php
Created March 15, 2016 11:33
Virtual field being called on save
App\Model\Entity\Product::_getStockApiUniqueCode() - APP/Model/Entity/Product.php, line 109
Cake\ORM\Entity::get() - CORE/src/Datasource/EntityTrait.php, line 294
Cake\ORM\Entity::has() - CORE/src/Datasource/EntityTrait.php, line 340
Cake\ORM\Entity::offsetExists() - CORE/src/Datasource/EntityTrait.php, line 473
App\Model\Table\ProductsTable::beforeSave() - APP/Model/Table/ProductsTable.php, line 138
Cake\Event\EventManager::_callListener() - CORE/src/Event/EventManager.php, line 390
Cake\Event\EventManager::dispatch() - CORE/src/Event/EventManager.php, line 356
Cake\ORM\Table::dispatchEvent() - CORE/src/Event/EventDispatcherTrait.php, line 78
Cake\ORM\Table::_processSave() - CORE/src/ORM/Table.php, line 1439
Cake\ORM\Table::Cake\ORM\{closure}() - CORE/src/ORM/Table.php, line 1391
@half2me
half2me / DeliveriesController.php
Last active January 27, 2016 20:53
Associated model not getting linked with custom finder method
<?php
// In my controller:
$delivery = $this->Deliveries->get($id, [
'contain' => ['Warehouses', 'DeliveryNotes', 'Orders', 'Products' => function ($q) {
return $q->find('filtered', ['options' => ['group' => 'ean']])
->select(['Products.id', 'ProductInfo.sku', 'ProductInfo.id', 'Products.size', 'Products.product_info_id'])
->contain('ProductInfo');
}]
]);
@half2me
half2me / $query
Created December 30, 2015 19:57
$query in SQL code
SELECT
Products.id AS `Products__id`
FROM
products Products
INNER JOIN websites Websites ON Websites.id = 1
INNER JOIN products_websites ProductsWebsites ON (
Products.id = (ProductsWebsites.product_id)
AND Websites.id = (ProductsWebsites.website_id)
)
INNER JOIN product_info ProductInfo ON (
@half2me
half2me / controller.php
Created December 30, 2015 15:42
Extract where
$allowedIds = $this->Websites->Products->find()
->extract(['id'])
->where(['reservation_id IS' => null]) // Product is not already reserved
->where(['size' => $converter->convert($request['size_eu'], $gender['gender'])]) // Product size matches
->where(['id IN' => TableRegistry::get('ProductsWebsites')->find() // Product belongs to the Website
->select(['product_id'])
->where(['website_id' => $website->id])
])
->where(['product_info_id IN' => $this->Websites->Products->ProductInfo->find() // Product sku matches
->select('id')