Why CakePHP is not allowing to create MySQL table names without plural? - mysql

Putting "_ underscore" is old school. Why CakePHP is using it till now. And also why CakePHP is not allowing to create MySQL table names without plural. Is this mandatory ?

This is the default naming convention for cakephp. If you don't want to use default then use this method
For cakephp 3 details here
$this->setTable('my_table');
// Prior to 3.4.0
$this->table('my_table');
For cakephp 2 details here
class Example extends AppModel {
public $useTable = 'exmp'; // This model uses a database table 'exmp'
}

Related

How to specify a specific connection (or the database name) for Yajra Datatables in Laravel 5

I'm using Laravel 5 with Jajra/Datatables
The project use 3 databases, one is MySQL and 2 are commercial SQL databases.
The SQL databases have the tables with exactly same names.
If I want to display a table from MySql database I use in controller:
return Datatables::of(DB::table('coeficientVR_VanzariNoi')
->get(['id','marca','model','capacitate','combustibil', 'caroserie', 'altaClasaSchimb', 'coeficient',
]))->make(true);
and it's working great!
How to specify a table from one of the the SQL databases?
I have models associated to them, and models have the connection specified.
Example for one of table which is named "version":
class version_Jato extends Model
{
//
protected $connection = 'sqlJato';
protected $table = 'version';
protected $primaryKey = 'vehicle_id';
....
So I need to specify the SQL database but I don't know how.
Thank you for your time!
If you have already defined the $connection on each model you can query directly the model, i.e.:
return DataTables::eloquent(App\version_Jato::query())
->make();
You can read about it in the yajra/datatables docs.

Are there any way to create singular-name DB table?

development environment
Lnaguage : Golang ver.1.9.2
DB : mySQL
Framework : not decided (Maybe I'll use revel)
situation
I already have DB which has singular-name table ,like "user", "page". It can't be changed.
Now I'll develop new application using this DB.
I created simple application to connect this DB, and tried to auto migrate using gorm(https://github.com/jinzhu/gorm).
I defined some models, like "user" which is same as existing DB table name, and run auto-migrate just as it written in (http://jinzhu.me/gorm/database.html#connecting-to-a-database )
db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&User{})
Then, new table "users" was created.
Question
Can I create singular-name table, like "user" with auto-migrate or those things ?
Using gorm is not required, so I'll use another orm library if it works.
I hope anyone help me !
Implement function TableName on struct User to return a custom name for the table. The ORM uses this function to get the table name for all DB operations.
func (user *User) TableName() string {
return "user"
}
Refer docs here: http://jinzhu.me/gorm/models.html#table-name-is-the-pluralized-version-of-struct-name
You have set with db instance to use singular table, like this:
db.SingularTable(true)

Doctrine 2.5 - Generate schema provider class from existing database

Question
Are there any existing tools able to generate an implementation of Doctrine\DBAL\Migrations\Provider\SchemaProvider reflecting the state of an existing database?
I googled for a while and looked at similar questions in SO but i couldn't find an answer. They are all related to Symfony + Doctrine ORM.
Why do i need this?
I want to use doctrine migrations to manage/track changes on an existing database. I cannot jump into using doctrine ORM because I would need to refactor the database and it would break other ( non php ) applications that depend on it.
I know i can use migrations without ORM, but i need to provide a concrete implementation of Doctrine\DBAL\Migrations\Provider\SchemaProvider (documentation), in my case this would mean to rewrite the entire database.
If i had the SchemaProvider generated for the first time to reflect the state of the database. Eg
<?php
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Migrations\Provider\StubSchemaProvider;
final class MySchemaProvider implements SchemaProvider {
public function createSchema()
{
$schema = new Schema();
$table = $schema->createTable('foo');
$table->addColumn('id', 'integer', array(
'autoincrement' => true,
));
$table->setPrimaryKey(array('id'));
//and so on for the rest of the databse...
return $schema;
}
}
I would be able to edit the generated class and create migrations with:
$ ./doctrine migrations:diff

Can we fetch a table models from MySql using Nodejs ORM2 without defining all the properties/fields?

I would like to know if we can fetch a table model from MySql from Database, without defining all the properties.
I'm using Node.js ORM2
const orm = require('orm');
db.define('users', {
// donot want to specify all the properties/fields
});
Above users table has already been created in DB, so I just want to get that model and not create/specify all the properties in it.
We can do this with other ORM's like Objection.js - but can't change my current implementation.
Thanks in advance,
Unfortunately, you must define properties/fields while you are defining your Models. There aren't any other options.

Unable to create table in MySQL using Doctrine and Symfony2

I am working with Symfony2 and Doctrine ORM using MySql .
After creating an Entity, I am not able to create the table. It throws an exception.
anu#anu-bridge:/var/www/Symfony$ php app/console doctrine:schema:update --force --dump-sql
[Doctrine\DBAL\Schema\SchemaException]
The table with name 'product' already exists.
doctrine:schema:update [--complete] [--dump-sql] [--force] [--em[="..."]]
I tried to drop it , but still it throws the error.
anu#anu-bridge:/var/www/Symfony$ php app/console doctrine:schema:drop --force
Dropping database schema...
[Doctrine\DBAL\Schema\SchemaException]
The table with name 'product' already exists.
doctrine:schema:drop [--dump-sql] [--force] [--full-database] [--em[="..."]]
[Doctrine\DBAL\Schema\SchemaException]
The table with name 'product' already exists.
There is no tables in the database. Cleared all the cache for symfony and doctrine, but still the error is throwing.
Symfony2 version is 2.0.1 .
I've had a similar problem. Most likely you have two entities named Category inside different Bundles. For instance:
src/Acme/SomeBundle/Entity/Product.php
src/Acme/OtherBundle/Entity/Product.php
Comment one of these files and retry the console command.
I was getting this problem from a conflict with join table defined in an association class annotation and a join table defined in a ManyToMany annotation.
The mapping definitions in two entities with a direct ManytoMany relationship appeared to result in the automatic creation of the join table using the 'joinTable' annotation. However the join table was already defined by an annotation in its underlying entity class and I wanted it to use this association entity class's own field definitions so as to extend the join table with additional custom fields.
The explanation and solution was thanks to this post in the forum 'Doctrine Annotation Question'. This post draws attention to the Doctrine documentation regarding ManyToMany Uni-directional relationships. Look at the note regarding the approach of using an 'association entity class' thus replacing the many-to-many annotation mapping directly between two main entity classes with a one-to-many annotation in the main entity classes and two 'many-to-one' annotations in the Associative Entity class. There is an example provided in this forum post Association models with extra fields:
public class Person
{
/**
* #OneToMany(targetEntity="AssignedItems", mappedBy="person")
*/
private $assignedItems;
}
public class Items
{
/**
* #OneToMany(targetEntity="AssignedItems", mappedBy="item")
*/
private $assignedPeople;
}
public class AssignedItems
{
/**
* #ManyToOne(targetEntity="Person")
* #JoinColumn(name="person_id", referencedColumnName="id")
*/
private $person;
/**
* #ManyToOne(targetEntity="Item")
* #JoinColumn(name="item_id", referencedColumnName="id")
*/
private $item;
}
I got this error when editing my Product.orm.yml file.
I added a new manyToMany relation with a Category entity, and made a mistake on the joinTable line :
manyToMany:
categories:
targetEntity: Acme\ProductBundle\Entity\Category
inversedBy: products
joinTable:
name: Product # My mistake: joinTable should be something like ProductCategory
[...]
Indeed it's a silly error, I share anyway.
If you can, you can do this as this worked for me:
Drop the entire database:
app/console doctrine:schema:drop --force --full-database
Run all DB migrations:
app/console doctrine:migrations:migrate
I imagine It can happen quite often when copying entities. In my case it was a ORM table name annotation that was misfortunately duplicated.
/**
* #ORM\Entity()
* #ORM\Table(name="category")
* */
class Category {
I had this problem with an One-To-Many, Unidirectional with Join Table relation like (see doctrine doc). I did not find this error case with this relation type through the internet or stackoverflow, so i post here my solution for let help others with the same problem.
What caused this problem:
After reverse engineering the legacy db tables with ORM (see doc "How to Generate Entities from an Existing Database"), all tables also only join tables recieved an entity PHP class.
With annotations on a category entity i described the joining. Results this code:
/**
* #ORM\ManyToMany(targetEntity="Category")
* #ORM\JoinTable(name="category_child",
* joinColumns={#JoinColumn(name="category_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="category_child_id", referencedColumnName="id")}
* )
*/
public $children;
The #ORM\JoinTable(name="category_child" caused that doctrine wants to create this table again. One time because of the already existing Category_Child entity, and then the #ORM\JoinTable expression that points to the same table.
Solution
Solution was to delete the Category_Child entity that was created from reverse engineering. If you used Category_Child entity in some $em queries, you have to select those datas othwerwise. E.g. Through the parent that holds those child datas in an ArrayCollection, or over DBAL.