How can I retrieve field in Propel? - symfony-1.4

I need to get the whole field 'username' from my DB.
Can I do it with model class or Peer-class?
There are many ways to solve that using Query-class, but command propel:build-model didn't create Query-classes. I don't understand why.

Problem is solved. I use
$users = wiEdmsUserPeer::doSelect(new Criteria())
to get all elements.
Another way is:
$criteria = new Criteria();
$criteria->addSelectColumn($column_name);
wiEdmsUserPeer::doSelect($criteria);

Query classes weren't generated because you probably aren't using the latest version of Propel. Using Query classes is cleaner and works better than criteria, consider upgrading!

Related

OData WebAPI: How to hardcode OrderBy in Function?

I am using OData WebAPI (Microsoft.AspNet.OData). for some reasons, I need hardcore OrderBy in a function. for example and return like the following,
return DbContext.Companies.OrderByDescending(a => a.CompanyName);
When I executed the function, orderby hardcoded did not work, I also checked sqlprofile, it seem framework did something and remove it. If I use $orderby queryoption, it is no problem.
Is there anyone knows how to solve it? thks in advance.
One workaround to implement is by making CompanyName a part of the composite key and specifying the Column attribute. I doubt that it will work without making the property part of the key and just using the Column attribute but you can quickly try that out.
One other workaround is to insert an orderby clause in ODataQueryOptions in your controller.

Laravel dynamically set table name in model

I want to do horizontal partitioning for the "users" table which is having a large number of rows. So I split the table and will have then users_1, users_2 etc. These tables are generated dynamically.
My issue is how to set the table name dynamically in laravel models. I have tried below option and it works fine.
$hash = 1;
$user = new User();
$user->setTable('users_'. $hash);
$user->where('id', 23)->get();
Here I get the result from the users_1 table;
But when I call
User::all();
It is using the table users and not users_1.
I have also tried by using setTable() in the __construct method of model. But the issue is $hash is calculated based on the value used in controller which is not getting in the construct method of model.
Is there any solution for this?
You can make a scope that switches the "from" part of the query builder
add this in the model -
public function scopeFromTable($query, $tableName)
{
$query->from($tableName);
}
Then use
$DynamicTableData = ModelName::fromTable($tableName)->get();
you can use all Eloquent methods by this approach
It is due to User::all() is called statically getting a new Model class object and looking for table users by default. If you can use the instance you have created $this with setTable() then you can call with dynamic table names. $this refers the member variables and function for a particular instance.
Best solution for your case, would be to use the database level partitioning. So you don't need to manage the hashing. Just give a partitioning key static or range, with created_at field and then you can call to a single table with User::all() to get all the users and no need to call dynamically. Or can checkout database shard.
May be this can help you:
$data = new Model;
$data->setTable('users');
dump($data->get());
$data->setTable('users_status');
dump($data->get());
die;
Good Luck.

symfony3 doctrine2 association counter field

Consider the following case: I have two entities: Article and ArticleComment:
// \AppBundle\Entity\Article
/**
* #ORM\OneToMany(targetEntity="ArticleComment", mappedBy="article")
*/
private $comments;
I need to store the amount of comments in a field on the article (eg. articles.comments_count). The field needs to be updated whenever a comment is created or deleted.
Previously I used the CakePHP framework which has built-in CounterCache behavior which does this automatically. I've tried my best to find something similar for Doctrine 2 (starting with DoctrineExtensions library) but nothing seems to do what I'm looking for.
Any library that does this? Or do I have to come up with my own solution?
Edit: I've tried using Entity Events but I require this behavior on many entities so I'm interested in a reusable solution
You can take a look at the extra lazy associations. http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/extra-lazy-associations.html
This way you don't need to store the comment_counter as you will be able to use the count() function on your collection without loading the full collection.
Internally, Doctrine will issue a "select count" query.
Here is another answer which avoids storing this kind of aggregate and enables you to use the paginator as you've requested in comments. I didn't test it yet so there could be some errors.
$qb = $em->createQueryBuilder();
$qb
->select('a.title, a.author, count(c)')
->from('Article', 'a')
->leftJoin('a.comments', 'c')
->groupBy('a.id');
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate($qb, $page, $limit);
As I said, this issue is not really Doctrine related because your initial model design is bad.
Usually, you don't need to store an aggregate which can be computed with a count/groupby query.
This kind of aggregate is useful when you have a lot of joined entities which creates a real overhead during computing. Else, you don't need it.

How to get distinct paged list from SharpRepository FindAll method with selector?

I was trying to get a field from an entitytype which has many dups using the sharprepository FindAll method with paging. I don't know how to supply the Distinct parameter or if it is even possible?
repo.FindAll(spec, c => c.Field, new PagingOptions<EntityType>(1, 20, "Field", false);
At this point it's not something you can do, though it makes sense that we should add it to the list of things to add. If you wouldn't mind adding that as an issue to GitHub I'm sure we can add it pretty easily. We would just need to figure out the best way to include it.

Symfony2: establish a model relationship by id

I'm building an API on Symfony2 an I have a Model ItemOrder with a ManyToOne relationship to a Model Item. I have a few Items in my database, and I want to add an ItemOrder that points to an Item that is already in the database, and whose id I know. So my first approach is this:
$item = new Item();
$item->setId(2);
$orderItem = new OrderItem();
$orderItem->setItem($item);
$em->persist($orderItem);
$em->flush();
However, Symfony2 understand that I'm trying to create a new item. I know that a valid approach would be fetching the Item object with the entity manager, and then assign it to the ItemOrder, but I think it's a not very efficient way of doing it.
So how should this be done?
What you're looking for is called Partial Reference.
$item = $em->getPartialReference('Item', 2);
$orderItem = new OrderItem();
$orderItem->setItem($item);
$em->persist($orderItem);
$em->flush();
However, please read the What is the problem? paragraph carefully, it might be safer to query for full entities by ids instead.
getPartialReference() vs getReference()
I've originally also found what forgottenbas linked, but I don't think it's the correct solution.
These two methods seem to be almost identical and both are referenced in official documentation.
Seems that only reasonable way to determine which is best is by looking directly into source code: getReference() and getPartialReference().
Straight up you will notice that getPartialReference() is better documented with a clear description of a use case that exactly matches yours:
* The use-cases for partial references involve maintaining bidirectional associations
* without loading one side of the association or to update an entity without loading it.
If you investigate the code for getReferece() you will notice that in some cases it will result in a database call:
if ($class->subClasses) {
return $this->find($entityName, $sortedId);
}
and finally, getPartialReference() marks partial reference as read-only, better defining it's purpose:
$this->unitOfWork->markReadOnly($entity);
You can create special reference object. More info see on this question
$item = $em->getReference('FQCNBundle:Item', 2);
$orderItem = new OrderItem();
$orderItem->setItem($item);
$em->persist($orderItem);
$em->flush();

Resources