Cakephp Function in mode not executing - php

I have a function in my Comic Model as such:
<?php
class Comic extends AppModel
{
var $name = "Comic";
// Methods for retriving information.
function testFunc(){
$mr = $this->find('all');
return $mr;
}
}
?>
And I am calling it in my controller as such:
<?php
class ComicController extends AppController
{
var $name = "Comic";
var $uses = array('Comic');
function index()
{
}
function view($q)
{
$this->set('array',$this->Comic->testFunc());
}
}
?>
When I try to load up the page; I get the following error:
Warning (512): SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'testFunc' at line 1 [CORE/cake/libs/model/datasources/dbo_source.php, line 525]
Query: testFunc
And the SQL dump looks like this:
(default) 2 queries took 1 ms
Nr Query Error Affected Num. rows Took (ms)
1 DESCRIBE comics 10 10 1
2 testFunc 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'testFunc' at line 1 0
So it looks like, instead of running the testFunc() function, it is trying to run a query of "testFunc" and failing...

OK, I was finally able to reproduce the same error. I suspected the error was Cake trying to "automagically" create model functionality, which it does when it can't find a model matching the controller. So, what I did was create the controller (app/controllers/comic_controller.php) but not a model. I used the original poster's exact controller code, and when I tried to invoke the Comic::view action, I got the same error.
I can only conclude that, similar to the other departures from Cake's naming conventions, the original poster named the model file in a funny way (e.g. app/models/comics.php) such that Cake was unable to find it, and attempted to compensate with magic. And failed.
So, to underscore my first comment to the original question, I strongly recommend following the conventions! At the very least, start by following them, then break them one at a time, so that when something breaks, you know why.
For a "Comics" module, here are the basic components in the conventional naming scheme:
Controller:
class name: ComicsController extends AppController
filename: app/controller/comics_controller.php
Model:
class name: Comic extends AppModel
filename: app/controller/comic.php
Views directory: app/views/comics/
Database table: comics

What happens if you do a var_dump( $this->Comic->testFunc() )? That looks like it should work. Btw, can you really set $array like that? I thought array was a reserved word in PHP. Regardless, you may want to rename that to something more descriptive (the type is array, so you're not really adding any metadata that isn't already there with that name).
Additionally, what shows up in the CakePHP error log? Anything in the Apache error log?

Related

Yii2 select2 database exception number of bound variables does not match number of tokens

when i use select2 widget in my yii 2 applicaton, it will cause the following error:
SQLSTATE [HY093]: Invalid parameter number: number of bound variables does not match number of tokens
The SQL being executed was: SELECT * FROM `page` WHERE `page`.`id`=6
The executed sql does not apply my override in find function of Page model as follow:
public static function find()
{
return parent::find()->where(['websiteId' => Yii::$app->website->id]);
}
Debug stack hint me to beforeInsert() function in ActiveRecord at line 333 & this code:
$this->node->refresh();
Every thing seems ok, so whats wrong?
I had a same problem, It seems to be a bug in framework!
Just find refresh() function & change the following line in vendor/yiisoft/yii2/db/ActiveRecord.php file
$query->where($pk);
to
$query->andWhere($pk);

CakePHP producing MySQL error

I have the situation where I use the TableRegistry of CakePHP in a script that acts like a server. It first works, but if there was no trafic for around one day, the server still runs but once the TableRegistry is used, it produces the following error:
Warning Error: Error while sending QUERY packet. PID=2536 in [..vendor\cakephp\src\Databse\Statement\MysqlStatement.php, line 39]
Googling for the error results in:
MySQL have limitation for size of data
Which cannot be the case, since the query and the corresponding result is really small. I have the feeling that the CakePHP Mysql connection somehow times out internally.
Here is my coding for reporducing the error:
namespace WebsocketServer;
//load the cakePHP classes
require_once '../config/bootstrap.php';
use Cake\ORM\TableRegistry;
class Server {
public function query() {
//any data fetch mockup (the
$data = TableRegistry::get('Screens')->find()->all()->toArray();
}
}
$server = new Server();
//querying works perfecly
$server->query();
//wait for a day
sleep(60*60*24);
//querying results in an error
$server->query();

yii2 - how to truncate a table from console

I have created a console command and I need to truncate a table.
Reading the Class reference: http://www.yiiframework.com/doc-2.0/yii-db-command.html#truncateTable()-detail I am not able to understand what files I need to include in order to execute this command.
I am including:
use yii\db\Command;
use yii\db\Connection;
but not sure which one is correct.
And I have tried to execute:
$command = Yii::$app->db->truncateTable('user');
which gives me the following error:
Exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: yii\db\Connection::truncateTable()'
and:
Yii::$connection->createCommand()->truncateTable('user');
which gives me the following error:
PHP Fatal Error 'yii\base\ErrorException' with message 'Access to undeclared static property: Yii::$connection'
I really don't understand what I need to do.
Yii::$app->db->createCommand()->truncateTable('user')->execute();
Using yii2 migrate that default function
yii2 migrate
Step 1. Create a migrate
yii migrate/create truncate_table_xxx
Step2. Edit file xxx_truncate_table_xxx
Some thing like that
class m150101_185401_truncate_table_xxx extends Migration
{
$this->dropTable('xxx')
}
Alternatively one may use:
User::deleteAll();
assuming User is active model class.
This command shows the number of deleted records.
Note, that unlike truncate deleting all records will NOT reset autoincrement counter to 1.

How to use PDO::lastInsertId()?

$sql='INSERT INTO complaints(complaint_id) values(default)';
$db->query($sql,array());
//get newly added complaint_id
$complaint_id=$db->lastInsertId();
I used above code to get last insert ID from complaints table. But it gives an error like
Fatal error: Call to undefined method EMMACore\Utils\DBConnection::lastInsertId() in /h... Can anybody tell me what is wrong in my application.Thanks.
In most case, the PDO instance is a property of EMMACore\Utils\DBConnection, so check the source code and find it out.
Something like: $db->getDbh()->lastInsertId(); where getDbh (or something like that) returns the PDO instance.
Edit: After seeing your result of var_dump(), that is for sure.
class db extends PDO
you can refer to this link PHP PDO Wrapper Class for example.

problem with doctrine:build-schema

im using symfony with doctrine, and Im trying to generate the schema yml file from a db. I get an this error
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'group' at line 1. Failing Query: "DESCRIBE group"I have a table named group in my database, and I suspect that this is screwing it up.
Its not access problems as ive checked my db previleges. Any suggestions on what I can do? I have tried the quote-identifier attribute but with no luck : (
Im also struggling to find some good doctrine documentation. i cant seem to find where a list of attributes are, say for a creating a column in the schema.yml file. I tried reaching out to symfony forums but they are not responsive! Any help would be greatly appreciated! Thanks..
As mentioned group is reserved keyword in MySQL, so you need to escape its name. In your project configuration class (/config/ProjectConfiguration.class.php) configure Doctrine Manager to use quotes:
class ProjectConfiguration extends sfProjectConfiguration {
public function setup() {
//...
}
public function configureDoctrine(Doctrine_Manager $manager) {
$manager->setAttribute(Doctrine_Core::ATTR_QUOTE_IDENTIFIER, true);
}
}
Assuming you're using up-to-date MySQL, "group" is a reserved word:
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Categories