Reputation: 11
I am trying to make the relationship between the tables I read The documentations in cakephp3, and tons of post in stackoverflow, and still can’t get any result. I have two tables.
category (id, name, published, created, modified)
portfolio (id, category_id, name, created, modified)
I want to display every element of portofolio with category like this:
Category Cars
* element 1: Bmw
* element 2: Audi
* element 3: Ford
How can i make this relation and display?
In categories model I write :
$this->belongsTo('Portfolios', [
'foreignKey' => 'id'
]);
And this is the query:
$portfolios_category = $this->Categories->find()
->contain(['Portfolios'])
->where(['Categories.published' => 1])
->order(['Categories.created' => 'desc'])
->all();
Upvotes: 1
Views: 44
Reputation: 762
Just one correction :
$this->belongsTo('Portfolios', [
'foreignKey' => 'category_id'
]);
After that loop through $portfolios_category
,
hope you will get the result.
Upvotes: 1
Reputation: 521
Start using the command prompt. Go to your project_folder/bin/ and open command prompt.
Then type
cake bake model
then you will get the list of Table with model name, then type
cake bake model <ModelName>
ex:
cake bake model Category
cake bake model Portfolio
Upvotes: 0