Filter by a field that's a foreign key - yii

Using Yii2.
I have a table called 'calificacion' that has a foreign key 'alumno-id' that points to field id in table alumno.
The column defined in the GridView widget is:
[
'header' => 'Alumno',
'attribute' => 'alumno.id',
'value' => 'alumno.name'
],
And it's showing perfectly, but the filter, in the header of the column, is not appearing. I want to have a textbox for writing the name of the alumno and get filtered. How can I achieve that?
EDIT: Here're the files https://dl.dropboxusercontent.com/u/7059378/Desktop.zip

First declare an attribute in your SearchModel.
public $alumno_name;
In your search model's rule add:
[['alumno_name'], 'safe'],
Join with alumno relation in search method (supponsinbly there is a alumno relation in your Calificacion model):
$query = Calificacion::find()
->joinWith(['alumno alumno']);
To sort with $alumno_name add:
$dataProvider->sort = [
'attributes' => [
//Other attributes here
'alumno_name' => [
'asc' => ['alumno.name' => SORT_ASC],
'desc' => ['alumno.name' => SORT_DESC],
],
]
];
To filter this you have to add:
$query->andFilterWhere(['like', 'alumno.name', $this->alumno_name]);
Finally in your grid view add:
[
'attribute' => 'alumno_name',
'value' => 'alumno.name'
],
You can find more info here and here.
Also instead of header use 'label' => 'Alumno'.

You must first define a relation in Model of that table 'calificacion', like
public function getAlumno()
{
return $this->hasOne(Alumno::className(), ['id' => 'alumno_id']);
}
Than in the search model of Calificacion set that you are joining tables after validation, like
$query->joinWith('alumno');
Than set search like
$query->andFilterWhere([
'alumno.id' => $this.alumno_id
]);

Related

cakephp4 get 1st record from containing table with order by a field

I have cakephp4 project
having 1 to many relationship between Portfolios and PSnaps
I want to show all 'Portfolios' with one associated record from PSnaps where its PSnaps.status=1 and order=>['PSnap.order_at'=>'ASC']
I tried many things but getting the correct result
below is giving 90% correct result only ordering on PSnaps.order_at is not working.
along with hasmany() i have created hasOne() association as shown in below model
Model
class PortfoliosTable extends Table
{
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('portfolios');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->hasOne('FirstPSnaps', [
'className' => 'PSnaps',
'foreignKey' => 'portfolio_id',
'strategy' => 'select',//also tried join
//'joinType'=>'LEFT',//also tried inner,left,right
//'sort' => ['FirstPSnaps.order_at' => 'ASC'], //*******this is not working
'conditions' => function (\Cake\Database\Expression\QueryExpression $exp, \Cake\ORM\Query $query) {
$query->order(['FirstPSnaps.order_at' => 'ASC']);//*******also not working
return [];
}
])
;
$this->hasMany('PSnaps', [
'foreignKey' => 'portfolio_id',
]);
}
Controller
$pfolios = $this->Portfolios->find('all')
->select(['Portfolios.id','Portfolios.client','Portfolios.country','Portfolios.url'])
->where(['Portfolios.status'=>1])
->order(['Portfolios.order_at'=>'asc','Portfolios.id'=>'asc'])
->limit(8)
->contain([
'FirstPSnaps'=>function($q){
return $q
->select(['FirstPSnaps.portfolio_id','FirstPSnaps.snap'])
//->where(['FirstPSnaps.status'=>1])
//->order(['FirstPSnaps.order_at'=>'asc'])
;
}
])
->toArray();
it is returning correct porfolios with 1 p_snap record but ordering/sorting is not correct as I need first p_snap something like where p_snap.status=1 and p_span.portfolio_id= portfolios.id limit 1.

How to link Yii's Active Record model table with other table using JOIN

I am using Yii 2.
I have model User with table in DB.
I tried link this table with table auth_assignment which has columns user_id, item_name and created_at using LEFT JOIN clause. I wrote this:
$model = User::find()->leftJoin('auth_assignment AS a', '`a`.`user_id` = `user`.`id`');
Then I attach $model to ActiveDataProvider:
$dataProvider = new ActiveDataProvider([
'query' => $model,
'pagination' => [
'pageSize' => 5,
],
]);
But when I run application it prints only Users columns: screenshot
What I do wrong?
In User model you can't get attributes of another table(auth_assignment ).
You can do it by two ways.
Define relationship between User and auth_assignment table in User model using hasOne()
Example : ( use your model name for auth_assignment table)
public function getAuth()
{
return $this->hasOne(Authassignment::className(), ['id' => 'user_id']);
}
Than you can use auth_assignment table attributes as $model->auth->user_id;
Generate query as array as below, in this scenario you don't need to define relationship in model.
$model = User::find()
->select('user.*,a*')
->leftJoin('auth_assignment AS a', 'a.user_id= user.id')
->asArray();
$dataProvider = new ActiveDataProvider([
'query' => $model,
'pagination' => [
'pageSize' => 5,
],
]);
Now you can use attributes as array variable i.e.
$model['user_id'] etc.

CakePHP retrieving when related model NULL

I have a null-able relation model, and I am using this standard code to retrieve my records:
$this->paginate = [
'contain' => ['Clients']
];
$coupons = $this->paginate($this->Coupons);
I am getting just the records that have client associated.
what is the best practice to make the contain work like OR, and not AND
EDIT: the relationship is seted as the following:
$this->belongsTo('Clients', [
'foreignKey' => 'client_id',
'joinType' => 'INNER'
]);
I have solved it by setting the joinType to LEFT:
$this->belongsTo('Clients', [
'foreignKey' => 'client_id',
'joinType' => 'LEFT'
]);

Yii 2.0 Select Pre-selected values from Database

I have been trying to fix an issue but to no avail but i am sure i will find a solution here. I am using Kartik 2.0 Select extension to do a multiple select. Fine, all working when inserting into the database but i am unable to retrieve the saved records to be displayed as selected back in the select field.
//I have included the kartik widgets already
use kartik\widgets\Select2;
<label>Desired Specialization(s)</label>
<?= $form->field($spec, 'id')->label(false)->widget(Select2::classname(), [
'data' => $model->getAllSpecializations(),
'options' => ['placeholder' => 'You can choose more than one specialization ...'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true
],
]);
?>
</div>
Please, your reply will be appreciated. Thanks
I think you need to add the saved values as the initial data? Like so:
'value' => $savedDataArray, // initial value
http://demos.krajee.com/widget-details/select2#usage-tags
After much digging into the code, i found a way on how to display selected database values into a multi-select option using Yii Select2
My Model
public function getCandidateLanguage()
{
$langValues = (new \yii\db\Query())
->select('c.language_id AS id, l.lang_name')
->from('candidate_language c ')
->innerJoin('languages l','c.language_id = l.id')
->where('c.candidate_id='.$this->candidate_id)
->orderBy('c.language_id')
->all();
return \yii\helpers\ArrayHelper::map($langValues,'id','lang_name');
}
My View
use kartik\widgets\Select2;
<?php
//the line below is to fetch the array key of $model->getCandidateLanguage() array
$lang->id = array_keys($model->getCandidateLanguage()); // value to initialize
echo Select2::widget([
'model' => $lang,
'attribute' => 'id',
'data' => $model->getAllLanguages(),
'options' => ['placeholder' => 'Choose multiple languages'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'tags' => true,
],
]);
?>
Hope it help someone who is facing the same issue.

Yii2 use own sql query in gridview (gii)

I'm fairly new to Yii(2).
I need to show the total amount of a single product, ignoring the locations. As shown here:
Before:
After:
Now, I need to show this in the gridview that Gii automatically made. But with my own query inside of it.
So instead of this:
It should show the total amount.
I have no idea how to use my own query inside of that.. Any help please? It's a standard Gii CRUD, can post code if requested.
Create new method inside of your related model and call with $data
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
.....
# below $data is current Model initial
['label' => 'Count', 'format' => 'html',
'value' => function($data){return $data::getCreatedStaticFuntion($data->product_id);}
],
# or use like below
['label' => 'Count', 'format' => 'html',
'value' => function($data){
$sql = 'SELECT * FROM tbl_product WHERE id ='.data->product_id;
return \app\models\Product::findBySql($sql)->all();}
],
....