I want order by proyecto_id who is in my ProyectosCategoraisTareas and also order by my categoria id, this is my contain Tareas.Categorias.
I try this but not work.
$proyectostareas = $this->ProyectosCategoriasTareas
->find('all', [
'contain' => [
'Proyectos',
'Tareas',
'Tareas.Categorias' => [
'order' => [
'Tarea.Categoria.id' => 'DESC'
]
]
]
])
->where([
'activa' => '1'
])
->order([
'proyecto_id' => 'DESC'
]);
Try this:
$proyectostareas = $this->ProyectosCategoriasTareas
->find('all')
->where([
'activa' => '1'
])
->join([
'Proyectos',
'Tareas'])
->order([
'proyecto_id DESC',
'Tarea.Categoria.id DESC'
]);
Related
Code
$query = $this->Client->find()
->hydrate(false)
->limit($limit)
->page($page)
->select(['Client.id','Client.achternaam','Client.Voornaam','clvsstatus.clientopnamestatus_id',
'clstatus.naam','afd.afdeling_naam','clsrtopn.naam'])
->join([
'clvsstatus' => [
'table' => 'client_vs_clientsoortstatus',
'type' => 'INNER',
'conditions' => [
'clvsstatus.client_id = client.id',
'clvsstatus.datum_van <='=>'2016-10-21',
'clvsstatus.datum_tot >='=>'2016-10-21',
**'OR' => ['clvsstatus.datum_tot'=>'0000-00-00']**
],
],
'clstatus' => [
'table' => 'clientsoortstatus',
'type' => 'INNER',
'conditions' => 'clstatus.id = clvsstatus.clientopnamestatus_id',
],
'clvsafdeling' => [
'table' => 'client_vs_afdeling',
'type' => 'INNER',
'conditions' => [
'clvsafdeling.client_id = clvsstatus.client_id',
'clvsafdeling.client_id = clvsstatus.client_id '
],
],
'afd' => [
'table' => 'afdeling',
'type' => 'INNER',
'conditions' => 'afd.afdeling_afdelingid = clvsafdeling.afdeling_id',
],
'clvssrtopn' => [
'table' => 'client_vs_clientsoortopname',
'type' => 'INNER',
'conditions' => 'clvssrtopn.client_id = client.id',
],
'clsrtopn' => [
'table' => 'clientsoortopname',
'type' => 'INNER',
'conditions' => 'clsrtopn.id = clvssrtopn.clientopnamesoort_id',
]
]) ;
Resulting SQL
SELECT
Client.id AS `Client__id`,
Client.achternaam AS `Client__achternaam`,
Client.Voornaam AS `Client__Voornaam`,
clvsstatus.clientopnamestatus_id AS `clvsstatus__clientopnamestatus_id`,
clstatus.naam AS `clstatus__naam`,
afd.afdeling_naam AS `afd__afdeling_naam`,
clsrtopn.naam AS `clsrtopn__naam`
FROM
client Client
INNER JOIN client_vs_clientsoortstatus clvsstatus ON (
clvsstatus.client_id = client.id
AND clvsstatus.datum_van <= '2016-10-21'
AND clvsstatus.datum_tot >= '2016-10-21'
**AND clvsstatus.datum_tot = '0000-00-00'**
)
INNER JOIN clientsoortstatus clstatus ON clstatus.id = clvsstatus.clientopnamestatus_id
INNER JOIN client_vs_afdeling clvsafdeling ON (
clvsafdeling.client_id = clvsstatus.client_id
AND clvsafdeling.client_id = clvsstatus.client_id
)
INNER JOIN afdeling afd ON afd.afdeling_afdelingid = clvsafdeling.afdeling_id
INNER JOIN client_vs_clientsoortopname clvssrtopn ON clvssrtopn.client_id = client.id
INNER JOIN clientsoortopname clsrtopn ON clsrtopn.id = clvssrtopn.clientopnamesoort_id
LIMIT
15 OFFSET 0
COMMENT :
As you see in the above code and resulting SQL even when explicitly ask for an OR condition cake always makes it AND, can anyone help with this thnxs
You have to rewrite the conditions on your first join to the following:
'conditions' => [
'clvsstatus.client_id = client.id',
'OR' => [
'clvsstatus.datum_tot'=>'0000-00-00',
'AND' => [
'clvsstatus.datum_van <='=>'2016-10-21',
'clvsstatus.datum_tot >='=>'2016-10-21',
]
]
]
Let's say I have a table
Date Item Qty
15/1/2016 Item1 10
16/1/2016 Item1 20
16/2/2016 Item1 30
18/2/2016 Item1 10
And In the report I want to display like this
Month Qty
01-2016 30
02-2016 40
Please tell me how can I do this in Yii2.
I've tried the following in my SearchModel.
$query = Productsalesdetails::find()
->select(['DATE_FORMAT(date, "%m-%Y"),productname,sum(total) as total'])
->groupBy('DATE_FORMAT(date, "%m-%Y")');
For the query use letteral select and alias for date_format too..
$query = Productsalesdetails::find()
->select( 'DATE_FORMAT(date, "%m-%Y") as m_date, productname, sum(total) as total')
->groupBy ('m_date, productname');
Do the fact you have already the format in select you can use the alias in attributes
<?= GridView::widget([
'dataProvider' => $dataProvider,
'summaryOptions' => ['class' =>'dfenx_pagination_summary',],
'pager' => ['options' => ['class'=> 'pagination pull-right']],
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => '',
'label' => 'Month',
'value' => function ($model) {
return $model->m_date;
},
],
[
'attribute' => 'productname',
'label' => 'Item',
],
[
'attribute' => '',
'label' => 'Total',
'value' => function ($model) {
return $model->total;
},
],
....
]);
this should work without adding $var in model for alias ..
otherwise you your model you need
class YourModel extends \yii\db\ActiveRecord
{
public $m_date;
public $total;
.....
I need to do default sorting by id, we don't show id in grid that's why default sorting is not working here is my code for sorting
public function search($params)
{
$this->load($params);
$query = new \yii\db\Query;
$expression = new \yii\db\Expression('CASE WHEN b.status = 1 THEN "Active" WHEN b.status = 2 THEN "Inactive" END AS status');
$query->select(['b.image','bl.name',$expression,'b.brand_id'])
->from('brand AS b')
->join('INNER JOIN',
'brand_lang AS bl',
'bl.brand_id = b.brand_id AND lang_id = 1');
$query->andFilterWhere([
'status' => $this->status,
]);
$query->andFilterWhere(['like', 'name', $this->name]);
$command = $query->createCommand();
$data = $command->queryAll();
$dataProvider = new \yii\data\ActiveDataProvider([
'query' => $query,
'totalCount' => count($data),
'sort' => [
'attributes' => [
'name' => [
'asc' => ['name' => SORT_ASC, 'name' => SORT_ASC],
'desc' => ['name' => SORT_DESC, 'name' => SORT_DESC],
'default' => SORT_DESC,
'label' => 'Name',
],
'status' => [
'asc' => ['status' => SORT_ASC, 'status' => SORT_ASC],
'desc' => ['status' => SORT_DESC, 'status' => SORT_DESC],
'default' => SORT_DESC,
'label' => 'Status',
],
'brand_id' => [
'asc' => ['brand_id' => SORT_ASC, 'brand_id' => SORT_ASC],
'desc' => ['brand_id' => SORT_DESC, 'brand_id' => SORT_DESC],
'default' => SORT_ASC,
'label' => 'Brand',
],
'defaultOrder' => ['brand_id' => SORT_ASC]
],
],
'pagination' => [
'pageSize' => 20,
],
]);
return $dataProvider;
}
Can anyone please tell me what is solution for this ? I did lots of googling but didn't success
Your defaultOrder param should be in sort, not in attributes :
'sort' => [
'attributes' => [...],
'defaultOrder' => ['brand_id' => SORT_ASC],
],
Use $dataProvider->sort->attributes['attribute_name']:
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => ['brand_id' => SORT_ASC]
],
]);
$dataProvider->sort->attributes['name'] = [
'asc' => ['name' => SORT_ASC],
'desc' => ['name' => SORT_DESC],
];
I update an entity (so same id) and I get a validation error on a field which has a 'unique' rule.
I verified in the db that there is no an existing identical field and so I don't understand why I have this error as I update the record so obviously the field exists already because it's itself!
Is it a problem to have a 'unique' rule on patchEntity()?
[
'site_name' => '...',
'datas' => object(App\Model\Entity\User) {
'id' => (int) 653,
'owner_id' => (int) 611,
'extraemails' => [
],
'owner' => object(App\Model\Entity\Owner) {
'id' => (int) 611,
'company' => 'This is the Company name',
'created' => object(Cake\I18n\Time) {
'time' => '2015-06-06T18:07:17+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\Time) {
'time' => '2015-06-17T02:44:36+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'[new]' => false,
'[accessible]' => [
'email' => true,
'phone' => true,
'phone2' => true,
'fax' => true,
'company' => true,
'tva_number' => true,
'address' => true,
'postcode' => true,
'city' => true,
'country_id' => true,
'users' => true,
'username' => true,
'origin' => true
],
'[dirty]' => [
'email' => true,
'phone' => true,
'postcode' => true,
'city' => true
],
'[original]' => [
'email' => '...',
'phone' => '...',
'postcode' => '...',
'city' => '...'
],
'[virtual]' => [],
'[errors]' => [
'company' => [
'unique' => 'This company name exists already.'
]
],
'[repository]' => 'Owners'
},
....
EDIT patchEntity() call added
$user = $this->Users->patchEntity($site->users[0], [
'email' => $this->siteDatas['userEmail'],
'phone' => $this->siteDatas['ownerPhone'],
'role' => 'owner',
'active' => 1,
'extraemails' => $this->siteDatas['extraemails'],
'owner' => [
'company' => $this->siteDatas['ownerName'],
'email' => $this->siteDatas['ownerEmail'],
'phone' => $this->siteDatas['ownerPhone'],
'address' => $this->siteDatas['ownerAddress'],
'postcode' => $this->siteDatas['ownerPostcode'],
'city' => $this->siteDatas['ownerCity'],
'country_id' => $this->siteDatas['countryId'],
'web' => $this->siteDatas['ownerWeb'],
'origin' => 'sitra',
],
],
[
'validate' => 'import',
'associated' => [
'Extraemails',
'Owners' => [
'validate' => 'import'
]
]
]);
if ($user->errors()) {
// Here is where I get the 'unique' error
}
So again I verified that no other record in the table have the same 'company' name.
and as you can see in the debug() at the begining of my post, I have a owner->id and it's really the one I want to modify.
Here is the validator
public function validationImport(Validator $validator) {
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create')
->notEmpty('company')
->add('company', 'unique', ['rule' => 'validateUnique', 'provider' => 'table', 'message' => __("This company exists already.")])
...
])
;
return $validator;
}
//in my model
public function initialize (array $config)
{
$this->addBehavior('Timestamp');
$this->belongsTo('Users');
}
//in my controller
$query = $this->Articles->find('all')->contain(['users']);
/src/Controller/ArticlesController.php (line 39)
object(Cake\ORM\Query) {
'sql' => 'SELECT Articles.id AS `Articles__id`, Articles.title AS `Articles__title`, Articles.body AS `Articles__body`, Articles.created AS `Articles__created`, Articles.modified AS `Articles__modified`, Articles.is_delete AS `Articles__is_delete`, Articles.user_id AS `Articles__user_id`, Users.id AS `Users__id`, Users.username AS `Users__username`, Users.password AS `Users__password`, Users.role AS `Users__role`, Users.created AS `Users__created`, Users.modified AS `Users__modified` FROM articles Articles LEFT JOIN users Users ON Users.id = (Articles.user_id)',
'params' => [],
'defaultTypes' => [
'Articles.id' => 'integer',
'id' => 'integer',
'Articles.title' => 'string',
'title' => 'string',
'Articles.body' => 'text',
'body' => 'text',
'Articles.created' => 'datetime',
'created' => 'datetime',
'Articles.modified' => 'datetime',
'modified' => 'datetime',
'Articles.is_delete' => 'integer',
'is_delete' => 'integer',
'Articles.user_id' => 'integer',
'user_id' => 'integer'
],
'decorators' => (int) 0,
'executed' => false,
'hydrate' => true,
'buffered' => true,
'formatters' => (int) 0,
'mapReducers' => (int) 0,
'contain' => [
'users' => []
],
'matching' => [],
'extraOptions' => [],
'repository' => object(App\Model\Table\ArticlesTable) {
'table' => 'articles',
'alias' => 'Articles',
'entityClass' => 'App\Model\Entity\Article',
'associations' => [
(int) 0 => 'users'
],
'behaviors' => [
(int) 0 => 'Timestamp'
],
'defaultConnection' => 'default',
'connectionName' => 'default'
}
}
You are debugging the query and not the result. To see the contents you can try:
debug($query->toArray())