I'm trying to update the status on my tickets table to the value : 2.
Once I can create the comment... (is working.. :) ), I wanted to change the status to 2.
This is my ticket model and the following function:
public function addComment($id,$body,$solved)
{
$this->find($id)->status = 2;
$this->save();
$this->comments()->create([
'ticket_id' => $id,
'body' => $body,
'user_id' => auth()->id()
]);
}
You need to get your object first then you can Update it:
public function addComment($id,$body,$solved)
{
$ticket = $this->find($id);
$ticket->status = 2;
$ticket->save();
$ticket->comments()->create([
'ticket_id' => $id,
'body' => $body,
'user_id' => auth()->id()
]);
}
Try changing your code like this. Maybe this will fix the problem you're having:
public function addComment($id,$body,$solved)
{
$ticket = Ticket::find($id);
$ticket->status = 2;
$ticket->save();
$ticket->comments()->create([
'ticket_id' => $id,
'body' => $body,
'user_id' => auth()->id()
]);
}
Related
I'm trying to use on my view a search made by CreateCommand to fill my CGridView, but I still need to pass as a dataProvider a CActiveDataProvider object.
Is there any way to transform my CreateCommand object into CActiveDataProvider or to do the same query directly as a CActiveDataProvider?
This is the view
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'credenciado-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'afterAjaxUpdate' => "loadAfterAjax",
'rowHtmlOptionsExpression' => 'array("style"=>"cursor: pointer","data-row-id" => $data->cre_id, "class" => "row_grid")',
'columns' => array(
'cre_razao_social',
array(
'header'=>'Status',
'name'=>'cre_ativo',
'value'=>'Status::getStatus($data->cre_ativo)',
'filter'=>array(true => 'ATIVO',false => 'INATIVO'),
'filterHtmlOptions'=>array('class'=>'col-sm-2'),
),
),
)); ?>
This is the controller
$model = new Credenciado('getValoresCredenciadosByConvenioRank');
$model = new CArrayDataProvider($model, array());
// pe($model);
$modelEst = new Estabelecimento('search_adm');
// pe($model);
$modelEst->unsetAttributes();
if (isset($_GET['Estabelecimento']))
$modelEst->setAttributes($_GET['Estabelecimento']);
// $model->unsetAttributes();
// if (isset($_GET['Credenciado']))
// $model->setAttributes($_GET['Credenciado']);
$this->render('listar',array(
'model' => $model,
'modelEst' => $modelEst,
'mes1' => $mes1,
'mes2' => $mes2,
'meses' => $meses,
'produto' => $produto,
'data' => $format,
'datas' => $date_unique,
'series' => $series,
'ano1' => $ano1,
'ano2' => $ano2
));
}
This is the Model
$q = Yii::app()->db->createCommand()
->select("
cre.cre_razao_social,
count(op.ope_id) as linhas,
par.par_data_vencimento,
pro.pro_codigo,
SUM(
CASE
WHEN date_trunc('month',par_data_vencimento) < date_trunc('month',now()) AND pro_codigo != 2 THEN
par.par_valor
WHEN pro_codigo = 2 THEN
op.ope_valor_parcela
ELSE
op.ope_valor_parcela
END
) as total_carteira")
->from("operacao op")
->join('parcela par', 'par.par_operacao_ope_id = op.ope_id')
->join('pos po', 'po.pos_id = op.ope_pos_pos_id')
->join('rubrica_credenciado rub', 'rub.rub_id = po.pos_rubrica_credenciado_rub_id')
->join('credenciado cre','rub.rub_credenciado_cre_id = cre_id')
->join('produto pro', 'pro.pro_id = rub.rub_produto_pro_id')
->join('matricula ma', 'ma.mat_matricula = op.ope_matricula AND ma.mat_convenio_con_id = '.Yii::app()->user->convenioid.' ')
->where('op.ope_con_id = :con_id', array(':con_id' => $con_id));
$q->andWhere(
"(par.par_data_vencimento between :datamin and :datamax and ope_status in('E','S'))
OR
(par.par_data_vencimento between :datamin and :datamax AND par.par_num_parcela = op.ope_qtde_parcelas AND op.ope_status = 'Q')
",
array(':datamin'=>$datamin, ':datamax'=>$datamax));
if(Yii::app()->user->sistema != 1) {
$q->andWhere('rub_consignavel = true');
}
$q->order('par.par_data_vencimento', 'ASC');
//$q->andWhere(array('in', 'ope_status', array('E','S')));
$q->group('cre.cre_razao_social, pro.pro_codigo, par.par_data_vencimento');
$q->queryAll();
This is the error
exception 'CException' with message 'CArrayDataProvider and its behaviors
do not have a method or closure named "search".' in
/var/www/html/yii/framework/base/CComponent.php:266
Stack trace:
you are making a mistake in your view. In your controller you are already passing object of CArrayDataProvider (i.e $model) to your view, so you should not call search() function on $model variable. Instead you should create CArrayDataProvider object from CreateCommand in your model and pass the object of Credenciado as $model in your controller and call your query method in the view like $model-> getValoresCredenciadosByConvenioRank() to get the data provider. Then your view would look like this
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'credenciado-grid',
// call your query function to get data provider (this is the name of function that you have implemented in your model)
'dataProvider' => $model->getValoresCredenciadosByConvenioRank(),
'filter' => $model,
'afterAjaxUpdate' => "loadAfterAjax",
'rowHtmlOptionsExpression' => 'array("style"=>"cursor: pointer","data-row-id" => $data->cre_id, "class" => "row_grid")',
'columns' => array(
'cre_razao_social',
array(
'header'=>'Status',
'name'=>'cre_ativo',
'value'=>'Status::getStatus($data["cre_ativo"])',
'filter'=>array(true => 'ATIVO',false => 'INATIVO'),
'filterHtmlOptions'=>array('class'=>'col-sm-2'),
),
),
));
?>
Your controller would become this
$model = new Credenciado('getValoresCredenciadosByConvenioRank');
$model->unsetAttributes();
if (isset($_GET['Credenciado']))
$model->setAttributes($_GET['Credenciado']);
// pe($model);
$modelEst = new Estabelecimento('search_adm');
// pe($model);
$modelEst->unsetAttributes();
if (isset($_GET['Estabelecimento']))
$modelEst->setAttributes($_GET['Estabelecimento']);
$this->render('listar',array(
'model' => $model,
'modelEst' => $modelEst,
'mes1' => $mes1,
'mes2' => $mes2,
'meses' => $meses,
'produto' => $produto,
'data' => $format,
'datas' => $date_unique,
'series' => $series,
'ano1' => $ano1,
'ano2' => $ano2
));
}
And your model would return CArrayDataProvider instead of query result like this,
$queryResult = $q->queryAll();
// return data provider for view instead of query result
return new CArrayDataProvider($queryResult, array());
Hopefully this solution will solve your problem. Let me know if it works or if the problem still persists.
Hi folks! I'm trying to transfer data as array from the controller to the model, and then paste the data into the query builder, but the data must be in the same order as specified in the columns.
What options do I have?
And do you think this is a bad practice?
Controller:
$responseNotes = Model::factory('Notes')-> createTicket([
'description' => htmlspecialchars($_POST['description']),
'contact_id' => $_POST['contact_id'],
'pref_contact' => $_POST['pref_contact'],
'dog_id' => $_POST['document_id'],
'type' => $_POST['type'],
'owner_id' => Auth::instance()->get_user()->id,
'cc' => $_POST['cc-emails'],
'title' => $_POST['title']
]);
Model:
public function createNote(array $data)
{
$columns = [
'type',
'owner_id',
'cc',
'title',
'description',
'contact_id',
'pref_contact',
'dog_id'
];
if (!array_diff($columns, array_keys($data))) {
// All needed values exists
$result = DB::insert($this->NOTES, $columns)-> values($data)-> execute($this->SCHEMA);
}
return ($result) ? $result : false ;
}
Thanks to this answer. Solved this by:
// Order $data array according to $column.values
$orderedData = [];
foreach ($columns as $key) {
$orderedData[$key] = $data[$key];
}
$result = DB::insert($this->TICKETS, $columns)
-> values($orderedData)
-> execute($this->SCHEMA);
Why you don't use ORM Model?
in controller:
$responseNotes = ORM::factory('Notes')-> values([
'description' => htmlspecialchars($_POST['description']),
'contact_id' => $_POST['contact_id'],
'pref_contact' => $_POST['pref_contact'],
'dog_id' => $_POST['document_id'],
'type' => $_POST['type'],
'owner_id' => Auth::instance()->get_user()->id,
'cc' => $_POST['cc-emails'],
'title' => $_POST['title']
])
try{
$responseNotes->save();
} catch (ORM_Validation_Exception $ex) {
print_r($ex->errors('models'));
}
And don't use htmlspecialchars($_POST['description'])
In model class modify function (doc):
public function filters()
{
return array(
'description' => array( array('htmlspecialchars') ),
);
}
It looks like You have associative array with structure db_column=>value right? Than You can simply insert like this:
DB::Insert('table_name',array_keys($data))->values(array_values($data))->execute();
I created a custom find type, and am trying to paginate the results, but the paginator seems to be ignoring the findType setting. Can someone tell me what I'm doing wrong?
(CakePHP 2.X)
In my Controller:
public function list($username=null) {
$this->Paginator->settings = array(
'Question' => array(
'findType' => 'unanswered',
'conditions' => array('Question.private' => 0),
);
$data = $this->Paginator->paginate('Question');
$this->set('data', $data);
);
Custom find type setup in my Model:
public $findMethods = array('unanswered' => true);
protected function _findUnanswered($state, $query, $results = array()) {
if ($state == 'before') {
$query['order'] = array('Question.created DESC');
$query['conditions'] = array_merge($query['conditions'], array('Question.date_answered' => ''));
return $query;
$this->log($query);
} elseif ($state == 'after') {
return $results;
}
}
Edit
I can paginate the query if I remove $this->Paginate->settings, and replace it with this:
$this->paginate = array('unanswered');
However, I want to add some additional conditions., this doesn't work:
$this->paginate = array('unanswered' => 'conditions' => array('Question.user_id' => $id, 'limit' => 4)) );
Is this possible?
findType was added to the paginator component in CakePHP 2.3, I was on 2.0
http://api.cakephp.org/2.3/class-PaginatorComponent.html
I am trying to build my own node reference custom field - I know several projects out there already do this, I am building this in order to learn... :)
My problem is the autocomplete path, it's not being triggered, I have checked the noderefcreate project and implemented my solution based on that project. But still; nothing is being triggered when I check firebug.
Here is my code:
function nodereference_field_widget_info() {
return array(
'nodereference_nodereference_form' => array(
'label' => t('Node Reference Form'),
'field types' => array('nodereference'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
'settings' => array(
'autocomplete_match' => 'contains',
'autocomplete_path' => 'nodereference/autocomplete',
),
),
);
}
function nodereference_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
if ($instance['widget']['type'] == 'nodereference_nodereference_form') {
$widget = $instance['widget'];
$settings = $widget['settings'];
$element += array(
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['nid']) ? $items[$delta]['nid'] : NULL,
'#autocomplete_path' => $instance['widget']['settings']['autocomplete_path'],
);
}
return array('nid' => $element);
}
You need to define method from where auto compete will take suggestions.
It can be done like this:
/**
* implements hook_menu
*/
function your_module_name_menu() {
$items['master_place/autocomplete'] = array(
'page callback' => '_your_module_name_autocomplete',
'access arguments' => array('access example autocomplete'),
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Auto complete method implementation
* #param $string
*/
function _your_module_name_autocomplete($string) {
$matches = array();
// Some fantasy DB table which holds cities
$query = db_select('target_db_table', 'c');
// Select rows that match the string
$return = $query
->fields('c', array('target_column'))
->condition('c.target_column', '%' . db_like($string) . '%', 'LIKE')
->range(0, 10)
->execute();
// add matches to $matches
foreach ($return as $row) {
$matches[$row->city] = check_plain($row->city);
}
// return for JS
drupal_json_output($matches);
}
Controller:
$data =
array(
'ContentI18n' =>
array(
0 =>
array(
'title' => 'first',
'author' => 'first',
'source' => 'sgfsdfrst',
'lang' => 'fa',
),
),
'Content' =>
array(
'publish' => 1,
'type' => 3,
'pages' => 8,
'volume' => 7,
'modified' => '2012-05-27 14:16:37',
'created' => '2012-05-27 14:16:37',
'lang' => 'fa',
),
);
$this->Content->create();
$this->Content->saveAll($data);
Model:
public $hasMany = array(
'ContentI18n' => array(
'className' => 'ContentI18n',
)
);
beforeSave function in behavior:
public function beforeSave(Model $model) {
// Define the new Translate model
App::uses($model->name . 'I18n', 'Model');
$varI18n = $model->name . 'I18n';
$modelI18n = new $varI18n;
foreach ($model->data[$model->name] as $key => $data) {
if (!in_array($key, $this->setting))
$modelData[$model->name][$key] = $data;
else
$modelData[$model->name . 'I18n'][0][$key] = $data;
}
$modelData[$model->name . 'I18n'][0]['lang'] = $model->locale;
$modelData[$model->name]['lang'] = $model->locale;
$model->data = $modelData;
//pr($model->data);
return TRUE;
}
every things seems be cause when is save it directly it's save with saveAll. but when i use same structure of data in behavior did not work without any error.
I found out that beforeSave callback will not trigger by saveAll. for executing some code before saveAll we must override saveAll function in our model.
public function saveAll($data, $options = array()) {
/*
your code you want execute before saving...
*/
parent::saveAll($data, $options);
}
for other saving methods such as saveMany,saveAssociated,... beforeSave trigger by them.