Doctrine2 orderBy in Symfony4 UnitTest - doctrine-orm

I'm having an issue in Symfony when it comes to test my API. I want to get a field and its values that I insert with an orderBy in my DQL. I'm using a getSingleResult to get the result.
When I get it in Postman, the orderBy is functionnal.
When I try to get it in my console running php bin/phpUnit, the orderBy is inefficient. If I replace getSingleResult by getArrayResult, it works but I don't have an object and that's not what I want.
Is there someone who know's about a problem like this one ?

I found the answer : when I'm testing my API I'm adding the entity, then I try to get it, but the collection in the parent entity is kept in cache. The fact is Doctrine needs to know that in this particular case the entity must be refreshed so I told my entity manager to use the HINT_REFRESH. Now it's working just fine.

Related

Not possible to use shorthand route handlers if RestSerializer is used? (ember-cli-mirage)

I set up a simple Ember Twiddle to show you my error that is occurring when trying to update a model.
It's considerable that I'm using ember-cli-mirage for mocking the data.
According to the docs, I created a shorthand route that should handle the PUT request.
It does, but with the error: Your handler for the url /api/shops/1 threw an error: Cannot convert undefined or null to object
When using the JSONAPISerializer, everything is working with shorthands (mirage/config.js) and I'm able to update models, but in my case I have to use the RESTSerializer with serialized IDs in the responses.
The request payload when I'm sending the model's attrs are without Id at the end of the property name, f.e.:
// attrs object in PUT request
{
name: "Shop 1",
city: "1" // belongsTo relationship,
}
Now Mirage is trying to find those properties on the respective database model that has to be updated, but cannot find it, because in the database it's cityId and not just city...
I also found this issue report and it’s working, but I was hoping I could avoid something like this. As far as I can remember, in previous versions of ember-cli-mirage (v0.1.x) it was also not needed to override the normalize method in the serializer to be able to make use of the RestSerializer with serializedIds…
My question is:
Is there a way to stick to shorthand route handlers only, or do I really have to write a helper or other custom solution only because I have to use the RestSerializer?
That would be really sad, but at least I would know then.
Thanks for your support!
Short answer: it looks like you need the custom serializer for now until the bug fix for it is merged.
Long answer: that issue looks to be an issue that occurred in the 0.2 -> 0.3 upgrade for Mirage, likely because of underlying DB changes made in Mirage. It'll probably get fixed, but for now you'll need to work around it.

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).

WebRatio - fill list with custom query

I just started to use WebRatio and I am facing a simple problem, with easy solution if I was not using this fabulous framework .... I was asked to do some sort of glosary app, and what I am trying to do right now is a simple index where appears all letters from A-Z and how many entries starting with each letter there are in my database, quite simple.
I aleady started a database with my entity: name, description and last_modified. I had been looking for some information on how to do this but I found nothing. So what I am trying to do is to fill a list with a custom query that I made (it works good on my sql editor) but I am not able to list the results in a WebRatio list. I tried to use a "query" object linked to a "simpleList" object but when I run the app it gives me error: "Expected at least one element .. inside " I would appreciate any help or resource that explains how to do this.
Thanks in advance
You shouldn't link the query unit with list because the list will make it's own database query. Instead you should specify outputs for query unit and write your custom style template for query unit to visualize the results as html. Or also (not recommended) you can link the results of query unit with a script unit and output desired html within a script unit.

Yii dynamic model id

So I'm working on some unit tests and relational fixtures.
I'm creating a model dynamically like:
$model = CActiveRecord::model('Post');
$post = $model->findByPk(1);
But after that I cannot for some reason get $post->id. I traced the problem to CActiveRecord class:
public function __get($name)
{
if(isset($this->_attributes[$name]))
return $this->_attributes[$name];
...
Where $name = "id". It says that $this->_attributes[$name] does not exist! As a matter of fact _attributes is empty.
My Post class does not define id (or any other properties) as a public property and I don't want to do so either. I just let the AR map it to table columns for me.
What am I missing?
Edit 1
My fixtures are regular Yii fixtures - nothing really special about them.
What differs is the way I load them really. I extended the CDbFixtureManager to be able to specify the order in which they should be loaded by overloading load() method. Only thing of interest that actually fails is that in the fixtures that have foreign keys I use the following:
'comment1' => array('post_id' => $this->getRecord('Post', 'post1')->id);
That's where it fails. getRecord returns the actual Post record (since I know the Post fixture has already been successfully loaded and exists in DB), but on the ->id part I get an exception about that attribute not existing.
If I go into Post model and add public $id; to it, then everything works! But I'm not sure if it's good practice to go about declaring all properties public like that.
If you look at this page carefully:
http://www.yiiframework.com/doc/guide/1.1/en/test.unit
you'll see that they use an array form for retrieving fixtures:
$this->posts['sample1']['id']
There is an alias defined in their fixture array for each record and fixture items aren't loaded as models really ...
Does that help? If not, it would be helpful to see your fixture file :-)
I think I found the root cause of this issue for me. While my FixtureManager was using the testdb DBConnection, the models still used the regular one.
For whatever reason, my debugger was giving me misleading errors like the one described in my original post.
Once I was able to set the DBConnection of all Models in the unit test the puzzle snapped into place and everything is now working smoothly!

How to get an entity's key when using django-nonrel on App Engine

I'm using django-nonrel (http://www.allbuttonspressed.com/projects/djangoappengine) on Google App Engine. I have my models etc. setup and everything works great. I had one question though. I want to be able to access an entity's key. Given a model named 'Review', I do Review.objects.get(pk = 1).key() which throws an error (AttributeError).
Is there any way I can get the given entity's key so that I can use it instead of being forced to use the pk/id? I want to use the key as a unique identifier for unique & secret URLS (if someone has a better suggestion to achieve this, I'm all ears).
You can get id by calling object.key().id() which returns a unique key inside python .In templates you can simply call object.key.id . Then you can use get_by_id to get the object
You can get your primary key on django-nonrel from the meta data:
review._meta.pk
or for key name:
key=getattr(review,review._meta.pk.column)
Apparently the 'id' field changes depending whether your on the dev or production server. So this works!
reference:
http://www.b-list.org/weblog/2007/nov/04/working-models/