Should I use Connection::executeUpdate()? - doctrine-orm

I use Doctrine DBAL v2.5.0 and I would like to perform a simple update statement. In the documentation it is written that I should use the method executeUpdate() (http://doctrine-dbal.readthedocs.org/en/latest/reference/data-retrieval-and-manipulation.html#executeupdate) for that. But in the source code this method has the annotation #internal. Because of that I am not sure whether this method should be used from non-library code or not. Should I?

It seems that you have to use the executeUpdate() method on the doctrine service and not on the entity manager.
$this->container->get('doctrine.orm.entity_manager')->getConnection()->executeUpdate($query); gives a warning in my IDE that executeUpdate() is #internal.
$this->container->get('doctrine')->getConnection()->executeUpdate($query);
or in a controller $this->getDoctrine()->getConnection()->executeUpdate($query); does not give any warning.
With other words:
You want to call the executeUpdate() method on
\Doctrine\Bundle\DoctrineBundle\Registry class
instead of the
\Doctrine\ORM\EntityManager class
P.D. Maybe I should mention that I am using Doctrine in conjunction with Symfony2.

Related

Using UniqueEntity outside of Symfony stack

I have an app built on top of Silex and I'm using Doctrine as my ORM.
I have a problem where I'm trying to get a clean error for when a user tries to reuse an email, I found the validator UniqueEntity but it seems to be designed for the full stack version of Symfony.
My question is, is this true? I'm going a limb and assuming it as I haven't found anyone who successfully used it outside of Symfony.
My second question is, if I'm not able to use UniqueEntity, whats my next best option? I'm using the Symfony Validator component and would like to use something that's plugged into that to keep it all in the same block of code.
You can use UniqueEntity with Silex.
Here's the service provider package with the Doctrine ManagerRegistry implementation - saxulum/saxulum-doctrine-orm-manager-registry-provider. Also you can find the instructions how to use it with the UniqueEntity validator in README.
But you may want to implement you own UniqueEntity validator.
For example, if you want to validate DTO object (or any non-entity object), because it's not supported by Symfony's UniqueEntity validator (see issue on GitHub).

Symfony & Doctrine: Optional foreign key

Without scrutinizing why I want this (it may sound like a bad approach, but I have good reason) I want to know if there is a way in the standard-framework-edition 3.1+ to create a relational association to an entity that may not exist...
Firstly I do realize this determines the schema and that's fine. So if an entity does not exist, it doesn't create a foreign key and the field is always null, or if the target entity does exist, it creates the foreign key and the field works like a normal association...
Secondly, this only changes project to project, and may change down the line as an update to which I realize a manual schema update could be necessary.
Preferably without 3rd party bundle dependencies... hoping for the standard framework to do this,
Anybody?
Thanks in advance
Edit
I am using annotations in my entities with doctrine ORM
Furthermore
The simplest version of why I am doing this is because certain bundles are optional project-to-project, and bundle A may make use of entities in bundle B only if it is present. I have considered using services and if container->has then container->get, or the XML on-invalid="null" approach, but that doesn't address property persistence. I was happy with storing a non-mapped value as a custom relational field, which is fine, just lengthier and wondered if perhaps there was a way Doctrine could ignore a missing targetEntity...
Hm, perhaps I misunderstand your question, but this sounds like a normal 'nullable' association to me?
Create your assocation via annotation:
/**
*
* #var Child
* #ORM\ManyToOne(targetEntity="Child")
*/
private $child;
and use
setChild(Child $child = null)
{
$this->child = $child;
}
as a Setter to allow nullable values.
And your getter might look like:
getChild()
{
return $this->child;
}
In case there isn't any child it will return null.
I will keep the other answer as it responds to the question for a 'nullable association target' live data.
This is the answer for a 'nullable association target' meta data which is a different thing.
OP asks to provide a targetEntity in the metadata which cannot exist in his case, e.g. is not there in a different bundle (or whatever OP's mysterious reason might be).
In that case I recommend to build upon Doctrine's TargetEntityListener which is able to resolve the targetEntity during runtime and targetEntity can be set to an Abstract Class or an Interface:
/**
* #ORM\ManyToOne(targetEntity="Acme\InvoiceBundle\Model\InvoiceSubjectInterface")
* #var InvoiceSubjectInterface
*/
protected $subject;
InvoiceSubjectInterface will then be replaced during runtime by a specific class provided by config e.g.:
# app/config/config.yml
doctrine:
# ...
orm:
# ...
resolve_target_entities:
Acme\InvoiceBundle\Model\InvoiceSubjectInterface: AppBundle\Entity\Customer
So this should be eiter an extendable behaviour for providing no class or implementing an own solution.

Mocking Laravel's eloquent hydrated relationships

Every time I try to test a class that has a repository in it I came with the same question:
If an Eloquent class "hydrates" the protected array relations attribute after performing a query, I could in theory populate this attribute with a mock Collection/Resultset so when in my test I say something like $user->profiles (attribute without parenthesis) I could retrieve my mock data and test the model easily.
My question is: is there a way to make Mockery call the __call method without issuing an "attribute not found" error message?
So far, the only way I've achieved to do this is by applying the attribute directly to the mock object. The problem with this approach is that this way I can't assert if the data was read from the repository
If you need an example I can update the question, but I think I've been quite explicit ;)
Best regards
I'm new to all of this, but I've read you can use AspectMock to test an Eloquent model. Jeffery Way has a video explaining how it's done. Here is the original post explaining AspectMock at Codeception.

urlize() in Doctrine_Inflector Class in Doctrine 2 / Symfony 2?

I was looking for a built-in method to urlize/slugify a string, instead of copying a strandard one found on google.
Thus I found this : http://sourcecookbook.com/en/recipes/59/call-the-slugify-urlize-function-from-doctrine , referencing to this Doctrine Class http://www.tig12.net/downloads/apidocs/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Doctrine_Inflector.class.html , with the method urlize() which is exactly what I'm looking for.
But, in my Doctrine Bundle from Symfony 2, in \vendor\doctrine\common\lib\Doctrine\Common\Util my Inflector class is pretty empty.
What happened to this urlize() method ? Do I have to recode it ?
There's https://github.com/Behat/Transliterator which includes the urlize function from Doctrine1
This is the part taken from Doctrine 1.2.3
Doctrine inflector has static methods for inflecting text
You could just composer require behat/transliterator
and have a HelperClass extending Behat\Transliterator.
And then be able to do: MyStringHelper::urlize("isn't that great?")
The file you are looking at (Doctrine\Common\Util\Inflector) is supposed to be used internally by Doctrine, to convert between table names (underscore separated), property names (camelCase), and class names (CamelCase).
What you are looking for can be achieved with the sluggable doctrine extension. You can ingtegrate it easily into a symfony2 application with stof/StofDoctrineExtensionsBundle.

Doctrine2 apply function to query parameter

I use the doctrine query builder to build my query.
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select(array('u'))->from('Account', 'a');
-- problem here
$qb->where('lower_unaccent(u.email) LIKE :search');
$qb->setParameter('search', $search['search'] . '%');
This works fine, but I would also like to apply the lower_unaccent function to the search parameter.
Is there a way to do this with the query builder?
Because when I do LOWER_UNACCENT(u.email) LIKE LOWER_UNACCENT(:search) I get the following error:
Error: Expected Doctrine\ORM\Query\Lexer::T_STRING, got 'LOWER_UNACCENT'
Even if I change LOWER_UNACCENT TO LOWER, I get the same error message.
You have to use $qb->expr()->lower('u.email') and $qb->expr()->lower(':search'). To create the "LIKE" pass those as arguments to $qb->expr()->like().
There doesn't seem to be a unaccent method though, but maybe there is a way to customize this. Let me know how it went.
EDIT:
Looking into lower()'s code, it seems like you can use any function you want like this:
new Expr\Func('LOWER', array($x));
By default, I'm pretty sure you can't do that.
lower_unaccent is not standard SQL, and as far as I can see, not supported by Doctrine.
What you can do is extend DQL with user-defined functions, which will do whatever you want. It is not so hard to implement. Here is some documentation:
http://docs.doctrine-project.org/en/2.0.x/cookbook/dql-user-defined-functions.html
http://docs.doctrine-project.org/en/2.0.x/reference/dql-doctrine-query-language.html#adding-your-own-functions-to-the-dql-language