I am having dictionary as
array(
'ID1' => 1 ,
'ID2' => 2,
'Status' => 'Done'
)
While I have only 2 columns in my table, ID and Status. I want to update two rows having IDs 1, and 2, using only one time ($this->db->update). Is it possible or should I have to make custom query.
I want to do like ($this->db->update(where ID1 == 1 && ID2 == 2)).
Either
// Get your status
$item = array( 'Status' => $response['Status'] );
// unset status
unset($response['Status']);
// I assume except status rest all values are ids so
// array_values gives id
$this->db->where_in('id', array_values($response));
// Update table
$this->db->update('table', $item );
OR
You have to modify your array
$data = array(
array(
'id' => 1 ,
'Status' => 'Done'
),
array(
'id' => 2 ,
'Status' => 'Done'
)
);
and then
$this->db->update_batch('table_name', $data, 'id');
Like this you can modify
( for comment : Dear I cant do so, because I am getting response from some service. And I am not able to edit this response. )
[akshay#localhost tmp]$ cat test.php
<?php
$response = array(
'ID1' => 1 ,
'ID2' => 2,
'Status' => 'Done'
);
$data = array();
$item = array( 'Status' => $response['Status'] );
unset($response['Status']);
foreach($response as $new){
$item['id'] = $new;
$data[] = $item;
}
print_r($data);
// Here you update
// $this->db->update_batch('table_name', $data, 'id');
?>
Output
[akshay#localhost tmp]$ php test.php
Array
(
[0] => Array
(
[Status] => Done
[id] => 1
)
[1] => Array
(
[Status] => Done
[id] => 2
)
)
//this will update the values where ids equal any value in the second were_in parameter
//data is array of values to update
$data = [
'Status'=>'some status',
];
//array of ids to update
$ids = [1,2];
$this->db->where_in('id', $ids)->update('table_name', $data);
Related
If i try select without params it work fine
Log::debug(DB::connection('myconnect')->select('SELECT id FROM table WHERE city_id in ( '.implode(',', $cities)].' ) '));
Result is
local.DEBUG: array (
0 =>
(object) array(
'id' => 6606,
),
1 =>
(object) array(
'id' => 6611,
),
2 =>
(object) array(
'id' => 6631,
),
3 =>
(object) array(
'id' => 6861,
),
4 =>
(object) array(
'id' => 7163,
),
)
But if i try to use parameter
Log::debug(DB::connection('myconnect')->select('SELECT id FROM table WHERE city_id in ( ? ) ',[implode(',', $cities)]));
Only first record returned
local.DEBUG: array (
0 =>
(object) array(
'id' => 6606,
),
)
What am I doing woring?
Number of question marks has to be equal with the number of elements in $cities.
Or you could do it like this:
DB::table('table')
->whereIn('id', $cities)
->get();
I want something like this:
$res = Model::find('all', array(
'fields' => array(
'SUM(col1)' => array(
'alias' => 'col1_total',
),
'SUM(col2)' => array(
'alias' => 'col2_total',
)
)
);
expected generated SQL:
SELECT SUM(col1) AS col1_total, SUM(col2) AS col2_total
FROM `tbl` AS `Model` WHERE 1;
I tried many ways.
is this possible?
a working example for a single col:
$res = Model::find('all', array(
'fields' => 'SUM(col1)'
)
);
Cool!
working example:
$res = Model::find('all', array(
'fields' => array(
'SUM(col1) AS col1_total',
'SUM(col2) AS col2_total'
)
);
I'm using api soap v1 and calling catalog_product.list everytime I add the website_ids in filter It cause an error.
$filter = array(
'status' => array( '=' => 1 ),
'type_id' => array( '=' => 'simple' ),
'website_ids' => array('6'),
);
2nd question, the args of catalog_product.list is filter and storeView, if I add the store view id or code I display all the product I guess It ignores what I add on it.
$proxy->call($sessionId, 'catalog_product.list', $filter = null, '6');
Thanks
I found this solution.
//you're website's id
$result = $client->call($session, 'store.info', '6');
$code = $result['code'];
//here all filters, in 'filters' you can add others filters (like price for example)
$filters = array(
'filters' => array(
'status' => 1,
'type_id' => 'simple',
),
'storeView' => "$code"
);
try{
$result = $client->call($session, 'catalog_product.list',$filters);
} catch (Exception $e){
print_r($e);
}
print_r($result);
now it works? :)
I need to print out the result in CActiveDataProvider with CGridView and with pagination
The following is my function in model
public function getCompaniesJobsByCompanyId ( $companyId )
{
$criteria = new CDbCriteria(array(
'with'=>array(
'jobs'=>array(
'scopes' => array( 'offline' => array(0), ),
'vacancies'=>array(
'scopes'=>array(
'removed' => array(0),
'archived' => array(0),
),
),
),
),
'condition' => $this->getTableAlias(false) . '.company_id = ' . (int) $companyId,
)
);
$criteria->together = true;
$dataProvider = new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 20, //Yii::app()->params['pageSize'],
),
));
return $dataProvider;
}
How could be the CGridView to render my data?
By this way I iterate the result
$dataProvider = Employers::model() -> getCompaniesJobsByCompanyId(2);
foreach ( $dataProvider->getData() as $data ) {
echo $data['name'];
foreach ( $data->jobs as $jobs ) {
echo ' ---- ' .($jobs->employer_id) . ' ---- ';
foreach ( $jobs->vacancies as $vacancies ) {
echo '<br />' . ($vacancies->id) . '<br />';
}
}
}
And My view
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider' => $dataProvider,
'columns'=>array(
'title', // display the 'title' attribute
'id', // d
array(
'name'=>'job id',
//'value'=> '$data->jobs[0]["id"]',
//'value'=> 'jobs.id',
//'type' => 'raw'
),
array(
'name'=>'vacancy id',
//'value'=> '$data->jobs[0]->vacancies[0]->id',
//'value'=> 'print_r($data->jobs[0])',
'value'=> '$data->jobs["id"]',
//'type' => 'raw'
),
array(
'name'=>'employer name',
'type'=>'raw', // to encode html string
'value'=>'$data->name',
),
),
));
Any one can help me to print the values in jobs and vacancies relations?
UPDATE
I tried adding 'value' => '$data->jobs->id' but get an error Trying to get property of non-object
Update :
I tried 'value' => '$data->jobs[0]["id"]' and it display the the result correctly, but if there are only 1 record on the table. When there is more than 1 record on the table,
I need to print all result, how to loop on it ?
This line 'value' => '$data->jobs->id' raised an error Trying to get property of non-object because you have been permitted to accessed the property of object instead of array of objects (jobs)
The workaround is you declare a function to do the task on the controller which rendered your gridview
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
...
array(
'name'=>'newColumn',
//call the method 'gridDataColumn' from the controller
'value'=>array($this,'gridDataColumn'),
'type'=>'raw'
)
),
));
class MyController extends Controller
{
//called on rendering the column for each row
protected function gridDataColumn($data,$row)
{
$cellResult = "";
//do your loop here
//example
foreach ( $data->children as $child ) {
$cellResult .=$child->id . "<br/>";
$cellResult .=$child->name . "<br/>";
}
return $cellResult ;
}
...
}
Yii Tutorial
CGridView: Render customized/complex datacolumns
I Have a GetStudents function in Agent model. where I fetch the students related to the agents.
But I want to show more fields of students from another table
Eg
**Agent table** (agent has students)
agent_id
**student table**
STUDENTID pkey
agent_id
**relation table** (this table creates relation between student and household)
StudentID HOUSEHOLDID
**HOUSEHOLD TABLE**
HouseHOLDID Householdname
I want to fetch householdname when I fetch all student details.
really looking bazzare to me as I am newbie to YII
MY function in agent model.
public static function getStudents($id) {
$relationships = Relationship::model()->findAll('type = :type AND source = :agent_id', array(':type' => 2, ':agent_id' => $id));
//$relationships=sort($relationships);
// Generate relationship array
//print_r($relationships);
foreach ($relationships as $relationship) {
$in[] = $relationship->destination;
}
// Generate db condition
$criteria = new CDbCriteria;
if (! isset($in) || ! is_array($in) || sizeOf($in) == 0) {
$in[] = -999;
}
$criteria->addInCondition('StudentID', $in, 'OR');
return new CActiveDataProvider('Student', array(
'criteria' => $criteria,
'sort'=>array('defaultOrder'=>array(
'StudentID'=>CSort::SORT_DESC,
)),
));
}
My code to fetch data in by passing ID into it
<?php $this->widget('zii.widgets.CDetailView', array(
'data' => $model,
'attributes' => array(
array(
'label' => 'Agent Details',
'type' => 'raw',
'value' => '',
'cssClass' => 'heading',
),
'agent_id',
'user.email',
'first_name',
'last_name',
'company',
'phone',
),
)); ?>
Any help would be greatly appreicated.
Thanks
Ab
First you should make relation with student to relation table in relations array in relation model like this ..
'student'=>array(self::BELONGS_TO, 'Student', 'StudentID'), //after belongs to student is model class name
'household'=>array(self::BELONGS_TO, 'HOUSEHOLD', 'HOUSEHOLDID'),//after belongs to HOUSEHOLD is model class name
And then you can fetch all record with active record like this...
$relationships = Relationship::model()->with('student','household')->findAll('type = :type AND source = :agent_id', array(':type' => 2, ':agent_id' => $id));