CActiveDataProvider not resolving params in criteria - php

I'm following along with Agile Web Application Development with Yii 1.1 and PHP5, working on the "TrackStar" project. For the life of me I can't understand where my problem is.
I'm getting the Exception:
CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined. The SQL statement executed was: SELECT COUNT(*) FROM tbl_issue t WHERE project_id=:projectId
It appears (to this noob's interpretation) that the params in my CActiveDataProvider is some how not resolving in the criteria as in the view:
(in /protected/controllers/ProjectController.php)
55 public function actionView()
56 {
57 $issueDataProvider = new CActiveDataProvider('Issue', array(
58 'criteria' => array(
59 'condition' => 'project_id=:projectId',
60 'params' => array(':projectId=' => $this->loadModel()->id),
61 ),
62 'pagination' => array(
63 'pageSize' => 1,
64 ),
65 ));
66 $this->render('view',array(
67 'model'=>$this->loadModel(),
68 'issueDataProvider' => $issueDataProvider,
69 ));
70 }
I checked my code against a github repo and I couldn't find any differences anywhere.
Is there something straightforward I'm missing here without having to post all of my code (and have somebody actually look through it)?

It looks you have an error in params of your criteria. (= after :projectId).
Right statement is
'params' => array(':projectId' => $this->loadModel()->id),
Params array is list of query parameter values indexed by parameter placeholders. For example, array(':name'=>'Dan', ':age'=>'31').

Related

How do I use response from an API into the controller - CodeIgniter

I tried to integrate an API that built-in laravel and now I have to use that API's response into the Codeigniter project, I'm developing a Codeigniter project(I'm not familiar with this framework and it's my first time working with this),
I finally could do it, I'm getting a response from an API
$result = curl_exec($response);
$rs=json_decode($result,true); print_r($rs);
return $rs;
Array(
[user] =>
Array(
[user_no] => PMRQ3fVd6eGEvd5aO9MDJg()() [name] => Anthony [middle_name] => S [lastname] => Mosteller [email] => AnthonyMosteller#test.net
)
)
and desperately trying to access the object full name, email, and user_no into my controller
But I am stuck now.
controller code:
$user=$this->userapi->userapicall($user_url,$token);
$this->session->set_userdata('username',$user['name']); // here want to use full name (name+middle_name+lastname)
$this->session->set_userdata('useremail',$user['email']);
I'm getting an error for name and email
A PHP Error was encountered
Severity: Notice
Message: Undefined index: name
Filename: controllers/auth.php
Line Number: 106
Do I need to create for each loop or seems something really silly mistake I make
I would appreciate an example of this.
$rs = json_decode($result,true); print_r($rs);
Here you are getting a multidimensional array. As per your code sample it looks like -
$rs = ['user' => ['user_no' => 'PMRQ3fVd6eGEvd5aO9MDJg', 'name' => 'Anthony', 'middle_name' => 'S', 'lastname' => 'Mosteller', 'email' => 'AnthonyMosteller#test.net']];
So if are trying to access user's name in controller -
$this->session->set_userdata('username',$user['user]['name']);
This should fix the problem. Also you should check if you are getting success status from API.

Getting an error in php5.6 not in php5.5

I'm trying to get a phpdaemon running on ubuntu 16.04 with php5.6.
It runs on ubuntu 1404 with php5.5.9. I tried google of course but I don't know php code at all.
This is the error I'm getting:
PHP Warning: array_key_exists() expects parameter 2 to be array, null given in /../../Service.php on line 79
This is the the actual code on line 79 an a bit below:
if(array_key_exists("session_id", $createSession)) {
// Session ID.
$sessionId = $createSession["session_id"];
Would be great if someone could help me out.
EDIT:
thanks for the quick replies. someone asked for the $createsession. I think this is it, but I am not sure.
still, I'm not sure why it runs on the old version.
// Create and invoke.
$createSession = $this->call_service(P_XMLRPC_URL, "create_session", array(
"requester" => $sale['prkcode'] . '#' . $sale['pdcode'],
"service" => P_SERVICE,
"version" => P_VERSION,
"invoke" => array(
"method" => PRM_INVOKE_METHOD,
"parameters" => array(
"tickets" => array(
$ticket
)
)
)
));

Another yii - Trying to get property of non-object

sory another yii question, try this bust seems doesnt works.
I have this error, still learning yii
Trying to get property of non-object
D:\xampp\htdocs\mou\protected\views\mou\view.php(26)
Did i miss something? thanks before.
D:\xampp\htdocs\mou\protected\views\mou\view.php(26)
14 'value'=>Yii::app()->dateFormatter->format('dd/MM/yyyy',$model->tgl_perjanjian),
15 ),
16 array(
17 'name'=>'tgl_awal',
18 'value'=>Yii::app()->dateFormatter->format('dd/MM/yyyy',$model->tgl_awal),
19 ),
20 array(
21 'name'=>'tgl_akhir',
22 'value'=>Yii::app()->dateFormatter->format('dd/MM/yyyy',$model->tgl_akhir),
23 ),
24 array(
25 'name'=>'Mitra KY',
26 'value'=>$model->mitra_mous->mitra->nama,
27 ),
28 'lingkup_kerjasama',
29 'penandatangan',
30 'jabatan_penandatangan',
31 array(
32 'name'=>'file',
33 'type'=>'raw',
34 'value'=>CHtml::link($model->file,Yii::app()->baseUrl . '/files/mou/' .$model->file),
35 ),
36 ),
37 )); ?>
this is my models Mitra.php
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'jenis0' => array(self::BELONGS_TO, 'JenisMitra', 'jenis'),
'kab0' => array(self::BELONGS_TO, 'Wilayah', 'kab'),
'prov0' => array(self::BELONGS_TO, 'Wilayah', 'prov'),
'mitra_mous' => array(self::HAS_MANY, 'MitraMou', 'mitra_id'),
);
}
and this is models Mou.php
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'mitra_mous' => array(self::HAS_MANY, 'MitraMou', 'mou_id'),
'mitras' => array(self::HAS_MANY, 'Mitra', 'mitra_id','through'=>'mitra_mous'),
);
}
Change your line 26 to the code below.
use this code below
'value'=>$model->mitras->nama,
It seems that your mitra_mous relation is the problem. Due to the fact that the relation is a one-to-many relation, $model->mitra_mous will return an array. You cannot try to get a property from an array, so $model->mitra_mous->mitra is invalid. Maybe you can reference only one of the items of the array, say $model->mitra_mous[0]->mitra->nama.
I am not sure if this solves your issue, but at least that line should work.
Be careful with this solution anyway, because if $model->mitra_mous has no elements, the code will fail. Yo may have to add a condition for that case.
Hope I have helped.
Good luck!

twice() method doesn't work in Mockery PHP

I am new to PHP Mockery Framework. I have a mock function executePrepared($arg1, $arg2, arg3) which I am calling it twice but seems to be not working and gives below error in PHPUnit command line:
Configuration read from C:\xampp\htdocs\DatabaseTesting\phpunit.xml
..←[31;1mE←[0m
Time: 47 ms, Memory: 3.25Mb
There was 1 error:
1) Test\Database\Table\TableTest::testinsertMany
Mockery\Exception\NoMatchingExpectationException: No matching handler found for
Mockery_0_Database_PGC::executePrepared(array(0=>'ClayPipe',1=>2000,2=>2100,3=>1
,4=>'2000-01-01',5=>'{"1":"1","2":6,"3":8,"4":10}',), "insert_assets", "\Databas
e\Model\Asset"). Either the method was unexpected or its arguments matched no ex
pected argument list for this method
my test function is as below:
public function testinsertMany() {
$this->PGCMock->shouldReceive('executePrepared')->twice()->withArgs(array(
[array('Clay Pipe',2000,2100,1,'2000-01-01','{"1":"1","2":6,"3":8,"4":10}'), 'insert_assets', '\Database\Model\Asset'],
[array('Main Street',1000,1100,0,'2000-02-01','{"1":"1","2":6,"3":8,"4":10}'), 'insert_assets', '\Database\Model\Asset']))
->andReturn($expectedResult1);
$data1 = array('name'=>'Clay Pipe',
'hist_cost' => 2000,
'val_cost' => 2100,
'val_method' => 1,
'service_date' => '2000-01-01',
'tags' => '{"1":"1","2":6,"3":8,"4":10}'
);
$data2 = array('name'=>'Main Street',
'hist_cost' => 1000,
'val_cost' => 1100,
'val_method' => 0,
'service_date' => '2000-02-01',
'tags' => '{"1":"1","2":6,"3":8,"4":10}'
);
$actualResult = $this->tableMock->insertMany(array($data1,$data2));
}
I don't understand what's wrong here. Is my Syntax for calling mock function twice() with passed argument wrong? Could any body please guide me here?
twice() should be used when the same call (including arguments) is expected to be executed 2 times. Looks like you want to check 2 consecutive calls, each with distinct argument.
If that is the case, this would work:
$this->PGCMock
->shouldReceive('executePrepared')
->once()
->ordered()
->withArgs([
array('Clay Pipe',2000,2100,1,'2000-01-01','{"1":"1","2":6,"3":8,"4":10}'),
'insert_assets', '\Database\Model\Asset'
])
->andReturn($result1);
$this->PGCMock
->shouldReceive('executePrepared')
->once()
->ordered()
->withArgs([
array('Main Street',1000,1100,0,'2000-02-01','{"1":"1","2":6,"3":8,"4":10}'),
'insert_assets', '\Database\Model\Asset'
])
->andReturn($result2);

yii join table using relations deploy to Linux not work

I using curd to show CGridview I join 2 table in my computer it work
but when deploy to linux server not work what wrong? ,help me pls!!
in controller
myController
$model=new UserLogin('search');
$model->unsetAttributes();
$dataProvider = new CActiveDataProvider('UserLogin', array(
'criteria' => array(
'with'=>'group',
),
'pagination' => array(
'pageSize' => 20,
),
));
$this->render('admin', array(
'dataProvider'=>$dataProvider,'model'=>$model)
);
this error
Invalid argument supplied for foreach()
/data/home/nueng/www/html/framework/db/ar/CActiveFinder.php(466)
454
455 /**
456 * Performs lazy find with the specified base record.
457 * #param CActiveRecord $baseRecord the active record whose related object is to be fetched.
458 */
459 public function lazyFind($baseRecord)
460 {
461 if(is_string($this->_table->primaryKey))
462 $this->records[$baseRecord->{$this->_table->primaryKey}]=$baseRecord;
463 else
464 {
465 $pk=array();
466 foreach($this->_table->primaryKey as $name)
467 $pk[$name]=$baseRecord->$name;
468 $this->records[serialize($pk)]=$baseRecord;
469 }
470
Hum just a guess are you respecting the case in the model name group in your with criteria?
Linux is case sensitive so if you put group instead of Group in your relation name or in the model name of the relation an error could occur.

Categories