I remember to have done this before, but now it does not work and I can't get it out.
[
'label' => 'Sex',
'attribute' => 'gan_sex',
'filter' => [
'1' => 'Male',
'2' => 'Female'
]
],
The output is
1
2
2
1
instead of
Male
Female
Female
Male
What is the problem now? I'd swear I used it just the same way but ...
I do it like this
[
'label' => 'Sex',
'attribute' => 'gan_sex',
'filter' => [
'1' => 'Male',
'2' => 'Female'
],
// translate lookup value
'value' => function ($model) {
$gender = [
'1' => 'Male',
'2' => 'Female'
];
return $gender[$model->gan_sex];
}
]
Possible values for gan_sex must be restricted to 1 and 2.
Related
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'
]);
I just cant figure out how to get the day, the months and the year from the dates.
Here it is the eloquent
public function query(Detail $model)
{
return $model->newQuery()->leftjoin('fishers','fishers.id', '=','details.fisher_id')
->leftjoin('species','species.id', '=', 'details.species_id')
->leftjoin('purposes','purposes.id', '=', 'details.purpose_id')
->leftjoin('islands','islands.id', '=', 'fishers.island_id')
->leftjoin('preservations','preservations.id', '=', 'details.preservation_id')
->select('fishers.*','details.*','details.indate','islands.island_name','fishers.fisher_first_name','fishers.fisher_last_name','details.weight','species.species_name','purposes.purpose_name','preservations.preservation_name');
}
I have tried to use Month(details.indate) as Month, Year(details.indate) as Year but seems not working. I used date as the datatype for indate and when I do Month(details.indate) as Month I get this error:
SQLSTATE[42S22]: COLUMN NOT FOUND: 1054 UNKNOWN COLUMN 'Month(details.indate)' IN 'field list' (SQL: SELECT fishers.* , details.* , MONTH(details.indate) AS MONTH
This is my datatable columns
protected function getColumns()
{
return [
[ 'data' => 'purpose_name', 'name' => 'purposes.purpose_name', 'title' => 'Purpose' ],
[ 'data' => 'fisher_first_name', 'name' => 'fishers.fisher_first_name', 'title' => 'Fisher Name' ],
[ 'data' => 'preservation_name', 'name' => 'preservations.preservation_name', 'title' => 'Preservation Methods' ],
[ 'data' => 'species_name', 'name' => 'species.species_name', 'title' => 'Species Name' ],
[ 'data' => 'island_name', 'name' => 'islands.island_name', 'title' => 'Island Name' ],
[ 'data' => 'weight', 'name' => 'details.weight', 'title' => 'Weight' ],
[ 'data' => 'indate', 'name' => 'details.indate', 'title' => 'Month' ],
[ 'data' => 'indate', 'name' => 'details.indate', 'title' => 'Year' ],
];
}
can someone help me how to do it?
You need to use the DB::raw function to select expressions using Laravel Eloquent.
For example:
return $model->newQuery()->...
->select(DB::raw('Month(details.indate) as Month'), DB::raw('Year(details.indate) as Year'), 'fishers.*','details.*', ...)
Be sure to add use DB; at the top of your file.
I have one table like:
name
startDate
endDate
units
price
and I show them in a gridview for a given year as follows (index.php in views):
$gridColumns = [
'name',
[
'attribute' => 'january',
'format' => 'currency',
'value' => function ($data) {
if(date("m", strtotime($data["startDate"])) == 1) {
return ($data["price"]*$data["units"]);
}
else {
return "";
}
},
'options' => ['style' => 'max-width:20px;'],
'pageSummary' => true,
],
[
'attribute' => 'february',
'format' => 'currency',
'value' => function ($data) {
if(date("m", strtotime($data["startDate"])) == 2) {
return ($data["price"]*$data["units"]);
}
else {
return "";
}
},
'pageSummary' => true,
],
... until month 12
];
In my search model i have defined:
public $january, $february, $march, $april, $may, $june, $july, $august, $september, $october, $november, $december;
placed them as safe in rules and defining attributes in order to get grid header with sortable items
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 0,
],
'sort' => [
'defaultOrder' => [
'name' => SORT_ASC,
],
'attributes' => [
'name',
'units',
'price',
'january',
'february',
'march',
'april',
'may',
'june',
'july',
'august',
'september',
'october',
'november',
'december',
],
],
]);
but when i try to sort by one of the months it gives an error like:
exception 'PDOException' with message 'SQLSTATE[42S22]: Column not
found: 1054 Unknown column 'january' in 'order clause''
How could I sort by these calculated columns that doesnt exist in the database?
Thanks
Huston, I have a problem;
Problem
So, I have a EAV models in my database, Entity, Attribute, Value. The relations between are:
Production hasMany Entity -> Entity belongsTo Production
Entity hasMany Attribute -> Attribute belongsTo Entity
Attribute hasMany Value -> Value belongsTo Attribute
The table values
---value (INT 3) ,
---label (VARCHAR 65),
---id (INT 11 AI)
I Can save the relations in my database, but, my application needs to return the SUM of Value.value a specific production, example:
The final user, need the consolidate of as Production by month of created. So, now, my application return is this:
(int) 0 => array(
'Production' => array(
'myconditions' => 'HereIsNotAProblem'
),
'Entity' => array(
(int) 0 => array(
'id' => '11',
'label' => 'Acompanhamento Familiar',
'production_id' => '8',
'Attribute' => array(
(int) 0 => array(
'id' => '33',
'label' => 'Nascidos vivos no m?s',
'entity_id' => '11',
'Value' => array(
(int) 0 => array(
'id' => '1',
'label' => '< 2500g',
'value' => '0',
'attribute_id' => '33'
),
)
)
)
)
I need to return:
(int) 0 => array(
'Production' => array(
'myconditions' => 'HereIsNotAProblem'
),
'Entity' => array(
(int) 0 => array(
'id' => '11',
'label' => 'Acompanhamento Familiar',
'production_id' => '8',
'Attribute' => array(
(int) 0 => array(
'id' => '33',
'label' => 'Nascidos vivos no m?s',
'entity_id' => '11',
'Value' => array(
(int) 0 => array(
'id' => '1',
'label' => '< 2500g',
'SUM' => 'TOTAL_SUM',
'attribute_id' => '33'
),
)
)
)
)
How I do this?
So when you query your data you need to left join Values to Attributes and use SUM expression. You can see similar example here, but using COUNT: https://book.cakephp.org/3.0/en/orm/query-builder.html#using-leftjoinwith
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;
.....