Quick Tips: Doctrine 2 useful commands

Standard

Hi there! Using Zend Framework 2 With doctrine have been nice! But commands for update and validate schema are hard to keep it in mind! So on Root Dir:

Validate Schema: 

./vendor/bin/doctrine-module orm:validate-schema

Create Schema:

./vendor/bin/doctrine-module orm:schema-tool:create

Validate an Update:

./vendor/bin/doctrine-module orm:schema-tool:update –dump-sql

Apply an Update:

./vendor/bin/doctrine-module orm:schema-tool:update –force

Hope It helps some one!

Don’t forget to leave a comment!

MySQL select to file

Standard

So, the idea is to execute a query in MySQL but export the result to a CSV file.
MySQL gives you the statement INTO OUTFILE
Example:

Select * from table_name into outfile ‘/path/to/file.csv

Hope it helps.

select … for update (mysql)

Standard

Hi there!

Using mysql, I saw a awesome feature for concurrency in web applications. The reality is, big information systems needs to sharing data so, database can be a problem sometimes.

Here’s the deal:
I want to read a database record or update him too, without the risk of another database call modify him. Here we can use for update statement.

Simple Example:

You have a table that has for a primary key a int value (like a counter) and later update him:

– Start a Transaction
Start Transaction;

– build your select for the record
select * from table where key_value = ‘value_string’ for update;

–here you can include updare statement
update table ….. etc, etc etc

–just commit transaction to take effect and release table
– consider that other table calls will wait until you call commit statement
commit;

 

the concept is simple and is available on most of PHP frameworks (Zend Framework too)

Hope it helps.
leave a comment…

Quick tip: Zend Framework 2 Form error message: Array provided to Escape helper, but flags do not allow recursion

Standard

So… strange mess here… creating a new form this exception appeared:

Array provided to Escape helper, but flags do not allow recursion

Some opinions suggest that the problem is in the fields of date type … and even suggest this approach:

$form->get('dateField')->setValue($entity->getDateField()->format('Y-m-d'));

I had no such field type date … I noticed the mess I made in the form construct -> an missing argument … so … have attention to it! (shame on me lol)

Quick Tip: Using Doctrine2 and Zend Framework 2

Standard

So… after 2 minutes trying to solve a simple question with my application I had to post this. Search a Doctrine Entity based on entity association.
Example: I have contents and Content Entity has an “ManyToOne” association with ContentType Entity -> How to find Content Objects based on little search text of ContentType?

//On ContentType Model Class

public function SearchByName($name) {
$list = $this->objectManager->getRepository(‘Application\Model\Entity\ContentType’)->createQueryBuilder(‘c’)
->where(‘c.nameLIKE :name’)
->setParameter(‘name’, ‘%’ . $name. ‘%’)
->getQuery()
->getResult();

return $list;
}

And on Content Model search Function, assuming I want to handle multiple ContentTypes:

//Content Model Class

public function fetchByContentType($contentTypeList) {
$list = $this->objectManager->getRepository(‘Application\Model\Entity\Content’)->createQueryBuilder(‘c’)
->where(‘c.type in (:cnType)’)
->setParameter(‘cnType’, $contentTypeList)
->getQuery()
->getResult();

return $list;
}

This is awesome, we can just pass one array of entities and Doctrine just handle every connections!

Hope It helps!