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.
I dont have the same result when i var_dump an array in my controller and in my view. This is my controller:
public function index()
{
$getFestivals = $this->festival_model->getAllFestivals();
if (!empty($getFestivals)) {
foreach ($getFestivals as $festival) {
$countFestivalEdition = $this->festival_model->countFestivalEditionByFestivalID($festival->fID);
$festival->fCountEdition = $countFestivalEdition;
}
}
$this->data['getFestivals'] = $getFestivals;
#var_dump($this->data['getFestivals']);die;
$this->render_layout('festivals/index', $this->data);
}
The var_dump (juste before last line) return me:
0 =>
object(stdClass)[24]
public 'fID' => string '6' (length=1)
public 'fName' => string 'Festival2' (length=17)
public 'fPicture' => string '3df2c152d9dec657380c53b679d0b92b.jpg' (length=36)
public 'fCity' => string 'City2' (length=9)
public 'fCountry' => string 'France' (length=6)
public 'fCountEdition' => int 2
1 =>
object(stdClass)[25]
public 'fID' => string '8' (length=1)
public 'fName' => string 'Festival1' (length=19)
public 'fPicture' => string '73a267e6c047d507b66b3a1ab1fc0059.jpg' (length=36)
public 'fCity' => string 'City1' (length=16)
public 'fCountry' => string 'France' (length=6)
public 'fCountEdition' => int 1
On this var_dump i have my 'fCountEdition'. When i make var_dump on my view, i have this (var_dump($getFestivals);die;):
0 =>
object(stdClass)[24]
public 'fID' => string '6' (length=1)
public 'fName' => string 'Festival2' (length=17)
public 'fPicture' => string '3df2c152d9dec657380c53b679d0b92b.jpg' (length=36)
public 'fCity' => string 'City2' (length=9)
public 'fCountry' => string 'France' (length=6)
1 =>
object(stdClass)[25]
public 'fID' => string '8' (length=1)
public 'fName' => string 'Festival1' (length=19)
public 'fPicture' => string '73a267e6c047d507b66b3a1ab1fc0059.jpg' (length=36)
public 'fCity' => string 'City1' (length=16)
public 'fCountry' => string 'France' (length=6)
Why he dont parse my data fCountEdition ?
For informations, my render_layout look like this:
$this->load->view($view, $this->data);
You may try this simple way to send array to view as give example bellow.
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
$data=$this->db->query("SELECT * FROM table_name");
$result=$data->result_array();
$headerData = array(
"pageTitle" => "Home",
"stylesheet" => array("home.css")
);
$footerData = array(
"jsFiles" => array("home.js")
);
$viewData = array(
"viewName" => "home",
"viewData" => array('result'=>$result),
"headerData" => $headerData,
"footerData" => $footerData
);
//Sendig all Result Data to view by Storing into array as $result
$this->load->view('template',$viewData);
}
View
<?php
var_dump($result);
?>
It's because you're not referencing $getFestivals, instead you are passing the value of it to $festival and trying to assign it a new variable.
Basically, passing the value allows you to 'copy' the array/object/etc without modifying the original.
Referencing the original allows to to modify it directly from within the loop.
Do do it the way you've written the code, you need to reference the original instead.
foreach ($getFestivals as &$festival) { <-- add ampersand before $
$countFestivalEdition = $this->festival_model->countFestivalEditionByFestivalID($festival->fID);
$festival->fCountEdition = $countFestivalEdition;
}
I get protected array from this
$user_id = Mage::getSingleton('admin/session')->getData('user');
var_dump($user_id);die;
object(Mage_Admin_Model_User)[115]
protected '_eventPrefix' => string 'admin_user' (length=10)
protected '_role' => null
protected '_hasAvailableResources' => boolean true
protected '_eventObject' => string 'object' (length=6)
protected '_resourceName' => string 'admin/user' (length=10)
protected '_resource' => null
protected '_resourceCollectionName' => string 'admin/user_collection' (length=21)
protected '_cacheTag' => boolean false
protected '_dataSaveAllowed' => boolean true
protected '_isObjectNew' => null
protected '_data' =>
array (size=17)
'user_id' => string '1' (length=1)
I want to get 'user_id'.,and tried like that $id = $user_id['_data']['user_id']; but it returns null
Try the following
$userId = Mage::getSingleton('admin/session')->getUser()->getId();
or
$userData = Mage::getSingleton('admin/session')->getUser()->getData();
$userId = $userData['user_id'];
Using cakephp 2.3.8 I am attempting to setup a connection to a custom couchbase datasource I have made within another datasource. The datasource I am attempting to load functions properly on the rest of the site when being loaded via a model. I want to use the connection manager to load this datasource. This is what I have so far:
database.php file:
public $queriesCB = array(
'datasource' => 'CouchbaseSource',
'username' => 'queries',
'password' => '',
'bucket' => 'queries',
'prefix' => 'q_',
'expiry' => '1814400', //3 Weeks
'autoConnect' => true,
'database' => NULL ,
'persistent' => false
);
Within my other datasource trying to load couchbase datasource
$db = ConnectionManager::getDataSource("queriesCB");
When I debug $db I get this:
object(CouchbaseSource) {
description => 'Couchbase DataSource'
conObject => object(Couchbase) {
[private] _handle => resource
}
config => array(
'password' => '*****',
'database' => '*****',
'prefix' => '*****',
'datasource' => 'CouchbaseSource',
'username' => 'queries',
'bucket' => 'queries',
'expiry' => '1814400',
'autoConnect' => true,
'persistent' => false
)
prefix => 'q__'
connected => false
cacheSources => true
configKeyName => 'queriesCB'
[protected] _baseConfig => array()
[protected] _descriptions => array()
[protected] _sources => null
[protected] _transactionStarted => false
}
Now when I try to call this I get a error:
$db = ConnectionManager::getDataSource("queriesCB");
$db->Get('test');
Error: Call to undefined method CouchbaseSource::Get().
This is a custom method, the datasource is all custom to work best with couchbase and the Get method does function properly. How am I setting up this connection wrong in cakephp?
Edit:
I just tested it with the default database config to a mysql database, it fails as well. The question now would be what would be the best way to initialize a new datasource? should I have to load a model with that datasource attached? Example: have a couchbase model with the datasource as couchbase?
Edit: Here is some of the datasource
class CouchbaseSource extends DataSource {
public $description = 'Couchbase DataSource';
public $conObject = NULL;
public $config = NULL;
public $prefix = NULL;
public function __construct($config = array()){
// If no configuration is set we use the default
$this->config = $config;
// Setup the cache string that is used when building the string
$this->prefix = (isset($this->config['prefix']) ? $this->config['prefix']."_" : "");
if ($this->config['autoConnect']) {
$this->connect();
}
}
public function connect() {
if ($this->conObject !== true) {
try {
$this->conObject = new Couchbase("127.0.0.1:8091", $this->config['username'], $this->config['password'], $this->config['bucket'], $this->config['persistent']);
} catch (Exception $e) {
throw new MissingConnectionException(array('class' => $e->getMessage()));
}
}
return $this->conObject;
}
public function query($method, $params, $object) {
// If not connected... reconnect!
if(!$this->conObject) {
$this->connect();
}
$apiMethod = $this->__methodToClass($method);
if (!method_exists($this, $apiMethod)) {
throw new NotFoundException("Class '{$apiMethod}' was not found");
} else {
return call_user_func_array(array($this, $apiMethod), $params);
}
}
private function __methodToClass($method) {
return 'CB' . strtolower(Inflector::camelize($method));
}
public function describe(&$Model) {
return $this->description;
}
/////////////////////////////////////////////////
// Query Methods
/////////////////////////////////////////////////
public function CBadd($key = NULL, $value = NULL, $expiry = NULL, $persisto = NULL, $replicateto = NULL) {
return $this->conObject->add($key, $value, $expiry, $persisto, $replicateto);
}
The solution is to directly call the query method from the couchbasesource. Given the way that I setup the datasource, unlike linking it through a model, the query information was not automatically passed. My go around was to simple use this.
App::uses('ConnectionManager', 'Model');
$db = ConnectionManager::getDataSource("queriesCB");
debug($db->query('Get', array('test')));
I have the following setup:
Model: User
Model: Client
User hasAndBelongsToMany Client
Client hasAndBelongsToMany User
DB Tables:
Users
id
lots of fields
Clients
id
lots of fields
Cliens_Users
id
client_id
user_id
status
Everything is working just peachy but I am wondering if there is a better way to get the status field (associated with the HABTM table) with my Client model.
Here is what I'm doing:
ClientsController.php
public function view($id = null) {
$this->Client->id = $id;
if (!$this->Client->exists()) {
throw new NotFoundException(__('Invalid client'));
}
$this->Client->contain('User.id');
$this->Client = $this->Client->read(null, $id);
$this->set('client', $this->Client);
}
the var_dump on my $this->Client looks like this:
array
'Client' =>
array
'id' => string '1' (length=1)
'first_name' => string 'Matt' (length=4)
'last_name' => string 'Moses' (length=4)
'address_1' => string '123 S Happy Lane' (length=16)
'address_2' => string '' (length=0)
'city' => string 'Yourtown' (length=5)
'state' => string 'TX' (length=2)
'zip' => string '12345' (length=5)
'phone_home' => string '8014567899' (length=10)
'phone_work' => string '8014567899' (length=10)
'phone_cell' => string '8014567899' (length=10)
'email' => string 'matt#matt.com' (length=13)
'created' => string '2012-06-13 04:41:19' (length=19)
'modified' => string '2012-06-13 04:41:19' (length=19)
'User' =>
array
0 =>
array
'id' => string '1' (length=1)
'ClientsUser' =>
array
...
Clients/view.ctp
...
<?php echo $client['User'][0]['ClientsUser']['status']; ?>
...
To me this last line screams "there is a cleaner way" but I can't seem to find anything on the matter. Any thoughts?