Link two controllers get() cakephp - php

I would like to join two controllers.
I have a controller Articles with a view function :
public function view($id = null)
{
$article = $this->Articles->get($id, [
'contain' => []
]);
$this->set('article', $article);
$this->set('_serialize', ['article']);
}
Each article has an associated user. So I have a controller Users.
Now I can have the user_id for the article (in database).
Is it possible to access the username in the User table with the user_id in the Articles table? With a get() function.
For example I know how to do it with find() :
$this->set('articles', $this->Articles->find('all')->contain(['Users']));
But is it possible with get()? For a particular article.
Thanks!

if you have username field in users table:
class UsersTable extends Table
{
public function initialize(array $config)
{
$this->displayField('username');
}
}
Articles table
class ArticlesTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
}
}
In your Articles controller:
public function view($id = null)
{
$article = $this->Articles->get($id, [
'contain' => ['Users']
]);
$this->set('article', $article);
$this->set('_serialize', ['article']);
}
in your articles view
<?= $article->has('user') ? $this->Html->link($article->user->username, ['controller' => 'Users', 'action' => 'view', $article->user->id]) : '' ?>

In the get() function, just add Users to the contain array:
'contain' => ['Users']

Related

Cannot search based on multiple id in find query in Cakephp 3

I have two tables category and product. I want to get details of product whose id are passed in conditions array of find query. The problem is when i pass condition as an array, it returns
Cannot convert value to integer
Secondly, if i debug it by passing only one id it returns category record, but it should return product record. Please help to solve my issue.
My code is as follows:
public function display()
{
$ids = array(1,2,3,4,5);
$c_List = $this->Products->Categories->find('all',array('conditions'=>array('Categories.category_id'=>$ids)));
$data = $c_List->toArray();
debug($data);
die();
}
Model - ProductsTable.php
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('products');
$this->setDisplayField('product_id');
$this->setPrimaryKey('product_id');
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Categories', [
'foreignKey' => 'category_id',
'joinType' => 'INNER'
]);
}
Model - CategoriesTable.php
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('categories');
$this->setDisplayField('category_id');
$this->setPrimaryKey('category_id');
$this->belongsTo('Categories', [
'foreignKey' => 'category_id',
'joinType' => 'INNER'
]);
}
When you are finding the records based on an array of ids, use IN in the condition clause.
public function display()
{
$ids = array(1,2,3,4,5);
$c_List = $this->Products->Categories->find('all',array('conditions'=>array('Categories.category_id IN'=>$ids)));
$data = $c_List->hydrate(false)->toArray();
pr($data);
die();
}

cakephp3- unable to show count of values

I am trying to show number of users present in a room. But when I try to do that, I get error.
This is the relationship.
Rooms has many users.
Rooms belong to groups.
public function initialize(array $config)
{
parent::initialize($config);
$this->table('rooms');
$this->displayField('name');
$this->primaryKey('id');
$this->belongsTo('Groups', [
'foreignKey' => 'group_id'
]);
$this->hasMany('Users', [
'foreignKey' => 'room_id'
]);
}
To achieve this, I wrote a controller function which looks like this:
public function index()
{
$this->loadComponent('Prg');
$this->Prg->commonProcess();
$params = $this->Prg->parsedParams();
$this->paginate = [
'limit' => 25,
'order' => ['id' => 'ASC'],
'finder' => ['RoomsList' =>['filter'=> $params]],
'sortWhitelist' => ['Rooms.id','Rooms.name'],
'extraOptions' =>['params' => $params]
];
$rooms = $this->paginate($this->Rooms);
$this->set(compact('rooms'));
$this->set('_serialize', ['rooms']);
}
This function paginates the list of rooms available. This is working fine but now when I try to show the count of users in each room, it throws error.
I modified my model function like this:
public function findRoomsList(Query $query, array $options)
{
$query->select(['id','name','modified','created'])
->contain([
'Groups'=> function ($query) {
return $query->select(['id','name']);
},
'Users' => function($query){
return $query->select(['status','full_name']);
//want to fetch total number of users
}
]);
echo $query;
return $query;
}
Now I'm getting this error:
You are required to select the "Users.room_id" field(s)
Can anyone tell me the mistakes I made here? I'm new to CakePHP3.

How to list objects in a hasMany relation in CakePHP 3?

I'm struggling with getting the data of a hasMany relation in CakePHP 3. I'm working on a basic forum and my current problem refers to the relation between categories and topics. A category contains of several topics, while each topic belongs to one single category. For both the categories and topics I used the bake mechanism and added the relation to the tables. This is the initialize method for the CategoriesTable class:
public function initialize(array $config) {
parent::initialize($config);
$this->table('categories');
$this->displayField('name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('Topics', [
'foreignKey' => 'category'
]);
}
And here's the same for the TopicsTable:
public function initialize(array $config) {
parent::initialize($config);
$this->table('topics');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Categories', [
'foreignKey' => 'category'
]);
}
Now I want to list the topics of one category like this (Categories\view.cpt):
<h1><?= $category->name ?></h1>
<table>
<?php foreach ($topics as $topic): ?>
<tr>
<td>
<?= $topic->name ?>
</td>
</tr>
<?php endforeach; ?>
</table>
How do I get the list of all topics related to the currently selected category?
Categories controller:
$topics = $this->Category->Topic->find('list', [
'keyField' => 'id',
'valueField' => 'Topic.category'
])
->where(['category' => $category]);
http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs
Thanks to bill for the hint. Here's my view method in the categories controller:
public function view($id = null) {
$category = $this->Categories->get($id, [
'contain' => []
]);
$this->set('category', $category);
$this->set('_serialize', ['category']);
$topics = $this->Categories->Topics->find()->where(['category' => $id]);
$this->set('topics', $topics);
}

How to store the relation between tables

I created a table in relation to two other tables, however I do not know how to relate them when creating new projects, how could I do that?
I'll create the categories in a separated page in my admin, and when the user create's a new project he will be able to select an array of categories coming from the table.
My question is, how can I store the relation when POST the data? I've never done this before.
Project model
class Project extends Model
{
protected $table = 'projects';
protected $fillable = [
'name',
'slug',
'header',
'desc',
'about',
'url',
'status'
];
public function customer()
{
return $this->belongsTo(Customer::class);
}
public function category()
{
return $this->belongsToMany(Category::class);
}
public function categories()
{
return $this->hasMany(Category::class);
}
}
Category model
class Category extends Model
{
protected $table = 'categories';
protected $fillable = [
'name',
'status'
];
public function subCategory()
{
return $this->hasMany(SubCategory::class);
}
public function projects()
{
return $this->belongsToMany(Project::class);
}
}
My actual Post create
public function postCreate(ProjectCreateRequest $request, Customer $customer)
{
//Array
$categories = $request->categories;
$customer->projects()->create([
'name' => $request->name,
'header' => $request->header,
'desc' => $request->desc,
'about' => $request->about,
'url' => $request->url,
]);
//How do I store the relation?
return redirect('admin/clientes/editar/' . $customer->id);
}
use attach or sync for many to many relationships
reference : https://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models
public function postCreate(ProjectCreateRequest $request, Customer $customer)
{
//Array
$categories = $request->categories;
$projects = $customer->projects()->create([
'name' => $request->name,
'header' => $request->header,
'desc' => $request->desc,
'about' => $request->about,
'url' => $request->url,
]);
//suppose here the $categories is an array of ids or a single integer variable id of the category that are related
$projects->categories()->attach($categories);
return redirect('admin/clientes/editar/' . $customer->id);
}
First: Make sure you have the following table in the database in order for the many to many relationship to work
table name : category_project
columns: category_id, project_id
Second: class Project needs only the following function for categories relation to work:
public function categories()
{
return $this->belongsToMany(Category::class);
}
Third: class Category needs only the following function for projects relation to work:
public function projects()
{
return $this->belongsToMany(Project::class);
}
Fourth: postCreate function should be as follow:
public function postCreate(ProjectCreateRequest $request, Customer $customer)
{
//should be an array of categories ids
$categories = $request->categories;
$project = $customer->projects()->create([
'name' => $request->name,
'header' => $request->header,
'desc' => $request->desc,
'about' => $request->about,
'url' => $request->url,
]);
//How do I store the relation? you can use attach or sync
$project->categories()->sync($categories);
return redirect('admin/clientes/editar/' . $customer->id);
}

CakePHP 3 join table associations

Alright, I have some issues understanding how the associations are working, particularly belongsTo here is my setup:
Articles can have multiple Categories
Categories can belong to multiple Articles
so in my database i have 3 tables:
articles, categories and a join table articles_categories
Table/ArticlesTable.php:
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
$this->table('articles');
$this->belongsTo('Users');
$this->belongsToMany('Categories', [
'through' => 'ArticlesCategories',
'alias' => 'Categories',
'foreignKey' => 'article_id',
'joinTable' => 'articles_categories',
'targetForeignKey' => 'category_id'
]);
}
Table/CategoriesTable.php:
public function initialize(array $config)
{
$this->table('categories');
$this->displayField('name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsToMany('Articles', [
'through' => 'ArticlesCategories',
'alias' => 'Articles',
'foreignKey' => 'category_id',
'joinTable' => 'articles_categories',
'targetForeignKey' => 'article_id'
]);
}
Table/ArticlesCategoriesTable.php:
public function initialize(array $config)
{
$this->belongsTo('Articles');
$this->belongsTo('Categories');
}
Now inside in the view action CategoriesController.php i can overview a particular category and i need to retrieve some articles related to that category.
What is the right way to do such a thing? Here is what i have:
public function view($id = null)
{
$category = $this->Categories->find('all',['limit'=>1])->where(['Categories.id' => $id])->contain(['Articles']);
$this->set(['category'=> $category]);
}
It kinda does the job but I'd also need to be able to limit the number of related articles..
you can modify the query object used to load the associated models:
$category = $this->Categories->find('all',['limit'=>1])
->where(['Categories.id' => $id])
->contain(['Articles' => function($q) {
$q->limit(10);
return $q;
}
]);
edit: or you can do
$category = $this->Categories->get($id,
[
'contain' => [
'Articles' => function($q) {
$q->limit(10);
return $q;
}
]);
or maybe if you want the Articles without the Category data you can use matching
$articles = $this->Categories->Articles->find()
->matching('Categories', function ($q) use $id{
return $q->where(['id' => $id])
->limit(10);
I did not tested the last one but I think something like that should work
But as you can see the complexity is more o less the same

Categories