problem with doctrine:build-schema - php

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

Related

Laravel GroupBy work in mysql but throws error in MariaDB

I'm Running the following query without any problem in MySQL (5.7.21) Database but this query throws error on my hosting server database which is MariaDB (10.1.47-MariaDB) and I don't have super permission to change the sql mode.
$projects = V2Project::leftJoin('v2_project_locations as v2pl', 'v2pl.project_id', 'v2_projects.id')
->leftJoin('v2_locations as v2l', 'v2l.id', 'v2pl.location_id')
->selectRaw('v2_projects.id, v2_projects.name, group_concat(v2l.name) as locations')
->withDepartmentInMind()
->onlyAuthorizedObjects();
if($by_current_step == true) {
$projects = $projects->where('current_step', 'plan');
}
if($search_val) {
$projects = $projects->whereIn('v2_projects.id', $search_val);
}
$projects = $projects->groupBy('v2_projects.id')->paginate(10);
$projects->appends(Input::except('page', '_token'));
Error:
SQLSTATE[42000]: Syntax error or access violation: 1055 'pm_form_moe.v2_projects.name' isn't in GROUP BY (SQL: select v2_projects.id, v2_projects.name from `v2_projects` group by `v2_projects`.`id`)
I also tried to add every column, but I need the functionality the same as mySQL without adding all columns.
Read about ONLY_FULL_GROUP_BY, then decide what to remove from your SELECT or what to add to the GROUP BY. Don't worry about setting the flag; your code is bad and needs to be fixed. And it was bad in the older version when it did not complain.
(If you show us the generated SQL, we can discuss it further.)

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.

Create new database and tables on the fly

I was reading the post "Create new database and tables on the fly"
I tried to implement an updated version of the create schema function:
public static function createSchema($schemaName)
{
$dbName = "db_{$schemaName}";
return DB::getSchemaBuilder()
->getConnection()
->statement("CREATE DATABASE :schema", ['schema' => $dbName ]);
}
but I get the following error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1 (SQL: CREATE DATABASE :schema)
PDO does not allowed to bind params on a create database query. So i'm not sure how to create DB's on the fly as safely as possible. Can anyone show me the way, before i have to introduce a security flaw in our site.
You cannot use bindings with CREATE DATABASE and PDO, because in CREATE DATABASE foo, foo isn't a quoted identifier, it's a raw new database name. You'll need to do any sanity checking yourself rather than relying on parameter binding to protect you.
public static function createSchema($schemaName)
{
$dbName = "db_{$schemaName}";
$quotedDbName = preg_replace("/[^_a-zA-Z0-9]+/", "", $dbName);
return DB::statement("CREATE DATABASE $quotedDbName");
}
See this question for more details about CREATE DATABASE with PDO binding.
public static function createSchema($schemaName)
{
// We will use the `statement` method from the connection class so that
// we have access to parameter binding.
return DB::getConnection()->statement('CREATE DATABASE :schema', ['schema' => 'db_' . $schemaName]);
}
i believe you dont need schema builder here

ZF2 Database table no instance returned

I am facing a problem with my database models in ZF2, I must have touch something in the application as I am sure it worked before.
Wondering if somebody can read out the problem by the follow error messages I've got.
If more error info is needed I can update this question with the stacks :)
Zend\ServiceManager\Exception\ServiceNotCreatedException
File:
/home/xxxxx/domains/xxxx.nl/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:909
Message:
An exception was raised while creating "Application\Model\CelebrityTable"; no instance returned
**Previous exceptions:**
Zend\Db\TableGateway\Exception\RuntimeException
File:
/home/xxxx/domains/xxxx/vendor/zendframework/zendframework/library/Zend/Db/TableGateway/AbstractTableGateway.php:105
Message:
This table object does not have a valid table set.
Well the exceptions says most of what you need to know.
File:
/home/xxxx/domains/xxxx/vendor/zendframework/zendframework/library/Zend/Db/TableGateway/AbstractTableGateway.php:105
Message:
This table object does not have a valid table set.
I went to Zend/Db/TableGateway/AbstractTableGateway.php:105 and the following piece of code is there;
if (!is_string($this->table) && !$this->table instanceof TableIdentifier && !is_array($this->table)) {
throw new Exception\RuntimeException('This table object does not have a valid table set.');
}
So your exceptions means. The $this->table is not a string, array or an instanceof Zend\Db\Sql\TableIdentifier
So you probably didn't set the table.
Now I never used the AbstractTableGateway so not sure how to use it in the right context. But I don't see a setTable, or something like a setOptions.
So unless you can show your implementation of your TableGateWay, this is as far as I know.
Note, I looked at zf2.3.3

Cakephp Function in mode not executing

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?

Categories