For an e-commerce app that I'm building I am using CakePHP.
I have created the db and afterwards baked my app with cake bake.
My models are all linked up properly as follows:
Product hasMany CategoryProduct,ImagesProduct
Category hasMany CategoryProduct
CategoryProduct belongsTo Product,Category
Image hasMany ImagesProduct
ImagesProduct belongsTo Product,Image
I have tried in various ways to obtain a paginated view of products dependent of the category_id with no succes.
I have managed to obtain the array I wanted with
$this->CategoryProducts->findByCategoryId($id), but I can't use the built-in paginator from cakePHP with the resultant array.
Could somebody tell me how to properly formulate the $options['joins'] array to pass on to the paginate function in order to get the same result, but in a paginated way?
The SQL for what I want to do would be something like this:
SELECT p . * , i.filename
FROM products p
LEFT JOIN (
category_products cp, categories c, images_products ip, images i
) ON ( cp.product_id = p.id
AND c.id = cp.category_id
AND c.id =2
AND ip.product_id = p.id
AND ip.image_id = i.id )
This is a question that perplexed me for quite sometime. You shouldn't have to associate either of your join models (CategoryProduct, ImagesProduct) directly to your models if you're using a HABTM association with CakePHP. cake bake may not have picked it up the HABTM association correctly if you didn't have the table names quite right. The join tables should be categories_products and images_products, which would make th join models CategoriesProduct and ImagesProduct.
Either way though, the following should get you going on filtering by categories:
//in products_controller.php:
//Fake a HasOne association, setting the reset parameter to false since pagination
//requires two queries to complete (one for the count, one for the data)
$this->Product->bindModel(array(
'hasOne' => array(
'CategoriesProduct
)
), false);
//Group by products since the HasOne will return multiple rows for each product
$options = array(
'group' => 'Product.id',
'conditions' => array(
'CategoriesProduct.category_id' => $categories
)
);
//To paginate the result:
$this->paginate = $options;
$products = $this->paginate();
In this example $categories can be either a single category_id or an array containing category_ids (i.e. array('1', '2', '3')). The result will be an 'OR' of categories. If you're looking to filter by an 'AND' type condition, check http://edblogs.cal.msu.edu/devteam/2011/02/08/cakephp-habtm-searches/.
Hope this helps.
Related
I try to use contain to make an full join of 2 Tables but i don't know how to change the condition:
$select= $this->Friends->find('all')
->order(['Friends.id' => 'ASC'])
->contain([
'Animals'
])
->where(['animal1_id'=> $animalsid,
'confirmed'=>'1'
]);
$this->set(compact('select'));
And so look the SQL:
SELECT
*
FROM
friends Friends
INNER JOIN animals Animals ON Animals.id = (Friends.animal2_id)
WHERE
(
animal1_id = 4
AND confirmed = 1
)
ORDER BY
Friends.id ASC
The problem is that this Friends.animal2_id i want to change with Friends.animal1_id but i don't know how? Maybe there another methods? I tried to use join but i get just one table not both.
I am considering that you have belongsTo relationship between the two model friends and animals.
So you just have to change the foreignKey name in the association, so update the association in your Friends model like
$this->belongsTo('Animals', [
'foreignKey' => 'animal1_id', //Here the column name you want to use for join
'className'=>'Animals',
'joinType' => 'INNER'
]);
EDIT(for comment query):-
If you want to select other tables, you can achieve this in two ways
In controller
$this->loadModel('OtherModelName'); //This will load your model to $this object
$other_table_result = $this->OtherModelName->find('all')->toArray();
Method to use raw SQL queries,
$sql = "SELECT * FROM users";
$query = $this->connection->prepare($sql);
$query->execute();
$result = $query->fetchAll();
Sorry if this is obvious - I'm trying to do a find() on a join table and failing, whats the correct syntax to do this?
Basically I have expenseCode table with a HABTM releationship with the expense table.
In my expenseCode model I have:
public function beforeDelete($cascade = false) {
$count = $this->Expense->find("count", array(
'conditions' => array('expense_code_id' => $this->id)
));
if ($count == 0) {
return true;
} else {
//$this->Session->setFlash('Record cannot be deleted as it has ' . $count . 'number of expenses attached to it');
return false;
}
}
If I uncomment the setFlash() I get the error.
Where am I going wrong? Thanks in advance.
Unfortunately it's not possible to perform such a query on the HABTM join table from one of the parent models without some extra work. I'm assuming that Expense is the parent model and ExpensesCode the join model?
A common way is to modelise the HABTM join table. Say you have an expenses and codes table which are joined by expenses_codes:
$this->Expense->bindModel(array('hasOne' => array('ExpensesCode')));
$this->Expense->find('all', array(
'fields' => array('Expense.*'),
'conditions' => array('ExpensesCode.expense_code_id' => $this->id)
));
However, Cake also auto-iniatlises a model for the join table when a HABTM association is defined (see the manual, and the "with" key in the list of possible keys).
So this syntax would let you directly query the join table:
$this->Expense->ExpensesCode->find('all', array(
'conditions' => array('ExpensesCode.expense_code_id' => $this->id)
));
The query above will net you with an array containing only the results from the join table, as it doesn't perform a join like the first procedure. So you would have to perform a second find() on the Expense model to find the expenses related to the expense_code_id from ExpensesCode.
EDIT:
It's a framework convention that HABTM join tables should be underscored and alphabetically ordered. So if the HABTM join table is called codes_expenses, it's auto-modelised as CodesExpense.
You can also work in a simpler way if you create another model with two hasMany relations instead of one with HABTM.
HasAndBelongsToMany between two models is in reality shorthand for three models associated through both a hasMany and a belongsTo association.
I found it is one of the simplest ways to save and retrieve data.
For more information:
http://book.cakephp.org/2.0/en/models/saving-your-data.html#what-to-do-when-habtm-becomes-complicated
I have a stores and store_details table and now a store_details_stores table.
The Store model has the following:
public $hasAndBelongsToMany = array('StoreDetail');
The StoreDetail model has the following:
public $hasAndBelongsToMany = array('Store');
When I attempt the below query in the stores_controller, I receive an sql error. For some reason the store_details table is not being joined naturally. Is this expected behaviour? Do I need to join this table manually?
$this->Store->find('all', array('conditions' => array('StoreDetail.name' => 'Parking')));
This is expected behavior. You need to set up a bindModel call with your conditions, or you can create a model for your join table and query that directly.
See:
CakePHP Book - HABTM
Modelizing HABTM Join Tables
$this->Store->bindModel(array(
'hasAndBelongsToMany' => array(
'StoreDetail' => array('conditions'=>array('StoreDetail.name' => 'Parking')
))));
$this->Store->find('all');
I would recommend applying the condition to the association before you do your find, using the bindModel method.
/**
* Apply a condition to the association
*/
$this->Store->bindModel(array(
'hasAndBelongsToMany' => array(
'StoreDetail' => array('conditions' => array('StoreDetail.name'=>'Parking'))
)));
/**
* Find all Stores that have an associated StoreDetail.name of 'Parking'
*/
$this->Store->find('all');
I'm new to cakePHP and I'm having issues getting all of my models to link up correctly.
This is the layout of my models in cakePHP:
Class belongsTo Location
Class belongsTo ClassType
Class hasMany ScheduledClass
ScheduledClass belongsTo Instructor
My problem is that when I use:
$this->Class->find('all')
I only get data from the Class, Location, ClassType and ScheduledClass models. I do not get any data from the Instructor model.
I can set the recursive value to '2' and retreive the data from the Instructor model, but this results in a giant amount of queries(one for every row) instead of a join on "ScheduledClass.instructor_id = Instructor.id".
What I was hoping to acheive is something like:
SELECT
...
...
FROM classes as Class
INNER JOIN locations as Location on Class.location_id = Location.id
INNER JOIN class_types as ClassType on Class.class_type_id = ClassType.id
INNER JOIN scheduled_classes as ScheduledClass on ScheduledClass.class_id = Class.id
INNER JOIN instructors as Instructor on ScheduledClass.instructor_id = Instructor.id
I've tried using both Containable and Joins in order to get the right data, but I wasn't able to get either to work(possibly due to my misunderstanding their uses).
Thank you in advance!
use Containable and set how you want the find to react and fetch what data you want.
in the Model
var $actsAs = array('Containable');
and in the Controller you can do something like this
$this->data = $this->User->find('first', array('conditions' => array('User.id' => $this->Auth->user('id')), 'contain' => array('City', 'Region', 'Country', 'UserOccupations', 'UserGroup')));
if you have your Model/Databases relationships set right you can pull the exact data you want even choosing which columns you wanna pull.
I have problem with updating (better updating not recreating) extra field in HABTM join table. I searched google and other sources, but struggled for 4 days now.
I have models:
class Tutorial extends AppModel {
var $hasAndBelongsToMany = array(
'TutorialCategory' => array(
'with' => 'TutorialCategoriesTutorial',
'order' => 'TutorialCategoriesTutorial.order_by ASC',
'unique' => true,
);
}
class TutorialCategory extends AppModel {
var $hasAndBelongsToMany = array(
'Tutorial' => array(
'with' => 'TutorialCategoriesTutorial',
'unique' => true,
);
}
join table tutorial_categories_tutorial have id, tutorial_id, tutorial_category_id, order_by fields.
I am trying to update order_by field like:
$order = 1;
foreach($tutorials as $i => $tutorial) {
$this->data[$i]['Tutorial']['id'] = $tutorial['Tutorial']['id];
$this->data[$i]['TutorialCategory']['id'] = $tutorial['TutorialCategory']['id];
$this->data[$i]['TutorialCategoriesTutorial']['order_by'] = $order;
++$order;
}
$this->Tutorial->bindModel(array('hasMany' => array('TutorialCategoriesTutorial')));
$saved = $this->Tutorial->saveAll($this->data);
This is deleting and crating new records in join table, but not setting order_by at all. I want to update record and set now order_by value. I tried hasMany through but no luck.
Please help and/or give advice and explanation.
Thank you!
As you have added extra data (order field) to the HABTM join model, you have actually exceeded the capabilities of a simple HABTM relationship with CakePHP. What you actually need to setup is a hasMany Through relationship.
In your case you'll basically make a "membership" model with Tutorial ID, catergory id and as much data as you want to assign to it. You will then define the relatioships as Membership belongsTo Tutorial & Category. The book probably has a better example than what I've just explained!
The main reason for this is that each "membership" record is treated as a normal record with no HABTM behaviour attached to it, so you can edit, delete and add records individually and easily.