How to specify default order by clause in relations - symfony1

comment:
tableName: comments
columns:
comment_id:
type: integer(4)
primary: true
notnull: true
autoincrement: true
news_feed_id:
type: integer(4)
relations:
newsFeed:
class: newsFeed
local: news_feed_id
foreign: news_feed_id
foreignAlias: comments
When I select newsFeeds and wanted to get comments for every newsFeed, Is it possible to get comments order by a particular column. I need to show the latest ones first. I hope my question is clear. I want to specify extra information in the relation to order by results of child table.

As you wrote you want to 'order your results'. Schema is not a right place for that. You do it in a query.
You didn't paste your full schema I guess. I assumed that you have created_at field in comment class (if not, try using Timestampable behavior):
Doctrine_Query::create()
->select('n.*, c.*')
->from('newsFeed n')
->innerJoin('n.comment c')
->orderBy('c.created_at DESC');

Related

Setting up the value of a field in a form taking from a Profile

I need some help here understanding how this works. See I'm using sfDoctrineGuardPlugin and add a Profile table as follow:
SfGuardUserProfile:
connection: doctrine
tableName: sf_guard_user_profile
columns:
id: { type: integer(8), primary: true }
user_id: { type: integer(8), primary: false }
idempresa: { type: integer(4), primary: false }
relations:
User:
local: user_id
class: sfGuardUser
type: one
foreignType: one
foreignAlias: SfGuardUserProfile
onDelete: CASCADE
onUpdate: CASCADE
Now I need to setup a field value based on logged in user in SdrivingEmisorForm, for that I need to access to idempresa on sf_guard_user_profile but I don`t know how :-( I tried all this:
$user = sfContext::getInstance()->getUser();
echo $user->getGuardUser()->getProfile()->getIdempresa();
echo $this->getUser()->getGuardUser()->getProfile()->getIdempresa();
echo $user->getProfile()->getIdempresa();
and none works, which is the right way to access to profile fields based on my schema definition? Can any take a brief and explain a bit how I must understand this in order to no get the same doubt if things changes some day?
EDIT
I've found the solution but using sfContext::getInstance() which many says is wrong, so in this case what is the right way to do this. Below th code works for me:
$user = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
PS: I'm access from SdrivingEmisorForm class
You are defining the foreignAlias as SfGuardUserProfile, so you should use this name for the getter.
In some action:
$profile = $this->getUser()->getGuardUser()->getSfGuardUserProfile();
In some template:
$profile = $sf_user->getGuardUser()->getSfGuardUserProfile();
Then:
echo $profile->getIdempresa();

Populate symfony admin generator's select field based on schema

I've inherited the development of a symfony 1.4 app (though It's my first symfony project).
After digging a bit on that app I've discovered a bug that is driving me nuts.
On the admin generator list page of a module there are two select fields in the filter form that should list each one a type of users (partners and clients), however both listings show all the users.
The generator.yml shows that both select fields should be populated by parter_id and client_id
[...]
filter:
display: [date, client_id, partner_id, ...]
form: ~
edit: ~
new: ~
[...]
Looking at the schema of the module it's obvious why symfony is populating the select fields with the same content because the relations of both client_id and partner_id are equivalent:
SomeModule:
tableName: client_requests
columns:
id: {type: integer, primary: true, autoincrement: true}
partner_id: {type: integer, notnull: true}
client_id: {type: integer, notnull: true}
date: {type: timestamp}
...
relations:
partner:
class: sfGuardUser
local: partner_id
foreign: id
type: one
client:
class: sfGuardUser
local: client_id
foreign: id
type: one
As per the above schema symfony has no other choice but to generate the select fields as:
select * from sf_guard_user;
The difference between partner and client is made by sfGuardGroup and sfGuardUserGroup, the related parts of the schema from ./plugins/sfDoctrineGuardPlugin/config/doctrine/schema.yml are:
sfGuardGroup:
actAs: [Timestampable]
columns:
name:
type: string(255)
unique: true
description: string(1000)
relations:
Users:
class: sfGuardUser
refClass: sfGuardUserGroup
local: group_id
foreign: user_id
foreignAlias: Groups
...
sfGuardUser:
actAs: [Timestampable]
columns:
first_name: string(255)
last_name: string(255
...
relations:
Groups:
class: sfGuardGroup
local: user_id
foreign: group_id
refClass: sfGuardUserGroup
foreignAlias: Users
Permissions:
class: sfGuardPermission
local: user_id
foreign: permission_id
refClass: sfGuardUserPermission
foreignAlias: Users
...
What I would like to is to generate those fields with a query like:
select u.*
from sf_guard_user as u
JOIN (sf_guard_user_group as ug JOIN sf_guard_group as g
ON (ug.group_id = g.id)) ON (u.id = ug.user_id)
where g.name = 'partner';
I've researched for a way to edit the module's schema to reflect the relation between client_id and partner_id with their group name to get the filter form select fields right, but I've ended believing that that is a wrong approach
¿is it possible to modify the module's schema to allow symfony to populate correctly the select fields?
¿or that approach is completely wrong and instead I should hack the methods that render those fields to populate them with the right queries? If so hints in that direction would be appreciated.
Thanks
Just edit your form (probably sfGuardUserForm) and add the query to the partner_id widget. Something like:
$query = Doctrine::getTable('sfGuardUser')->createQuery('u')->etc etc...
$this->widgetSchema['partner_id']->setOption('query', $query)
You'll probably need to do the same to the validator for that field:
$this->validatorSchema['partner_id']->setOption('query', $query)
If you cannot edit that specific form (maybe it is used in another part of the app), just make a new one that extends it. And then, you just need to specify the form class in the generator.yml file.

In doctrine what implications do 'relations:' have if the 2 relations are defined for two seperate column with a same table?

Suppose I have defined VendorClientLicense model like this:
VendorClientLicense:
tableName: vendor_client_licenses
columns:
id:
type: integer(4)
primary: true
notnull: true
autoincrement: true
status:
type: string(255)
default: 'pending'
client_id:
type: integer(8)
notnull: true
vendor_id:
type: integer(8)
notnull: true
relations:
sfGuardUser:
class: sfGuardUser
local: client_id
foreign: id
foreignAlias: VendorClientLicenses
foreignType: many
owningSide: true
sfGuardUser:
class: sfGuardUser
local: vendor_id
foreign: id
foreignAlias: VendorClientLicenses
foreignType: many
owningSide: true
indexes:
fk_vendor_client_licenses_sf_guard_user1:
fields: [client_id]
fk_vendor_client_licenses_sf_guard_user2:
fields: [vendor_id]
options:
charset: utf8
collate: utf8_unicode_ci
If you see the two relations are defined with same name 'sfGuarduser'; What I have found for this in mysql is that in generated database client_id does not show any association with sfGuardUser, whereas vendor_id does! If I change it to 'sfGuardUser1' and 'sfGuardUser2' then shows both relationship! So I assume eventually this has important significance and should not be identical for a same model. Is there any other implications for it?
Plus can you name me a good schema generator like 'mysqlworkbenchdoctrineplugin' which handles situation like this automatically?
Yes, you need to name them differently.
I asked & received for the same problem here:
MySQL: Two foreign keys in one table referring to another table
I haven't had any problems with it.
Regarding the plugin, sorry can't help you.

How do I access the list of tags linked to a blog post?

I'm doing a blog engine using symfony as a learning exercice.
How do I get the list of tags from the id of a blog post ?
Here's the database shema :
I added the following in the model :
public static function getTags($id)
{
return Doctrine_Core::getTable('Tag')
->createQuery('t')
->select('t.name, t.slug')
->leftJoin('t.ContentTag ct')
->where('ct.content_id = ?', $id)
->orderBy('t.name ASC');
}
and here is part of the schema.yml :
Content:
connection: doctrine
tableName: ec_content
actAs:
Sluggable:
fields: [title]
unique: true
canUpdate: true
Timestampable:
columns:
id:
type: integer(4)
fixed: false
unsigned: true
primary: true
autoincrement: true
(...)
relations:
Comment:
local: id
foreign: content_id
type: many
ContentTag:
local: id
foreign: content_id
type: many
ContentTag:
connection: doctrine
tableName: ec_content_tag
columns:
content_id:
type: integer(4)
fixed: false
unsigned: true
primary: true
autoincrement: false
tag_id:
type: integer(4)
fixed: false
unsigned: true
primary: true
autoincrement: false
relations:
Content:
local: content_id
foreign: id
type: one
Tag:
local: tag_id
foreign: id
type: one
It's difficult to tell without seeing exactly how your schema is defined (i.e. schema.yml), but my guess would be that this would work, assuming you have the content object loaded:
$tags = $content->Tags;
Otherwise, your code snippet should work, so far as I can tell. You just need to stick ->exec() on the end to make it return the results of the query rather than the query object itself:
return Doctrine_Core::getTable('Tag')
->createQuery('t')
->select('t.name, t.slug')
->leftJoin('t.ContentTag ct')
->where('ct.content_id = ?', $id)
->orderBy('t.name ASC')
->exec();
Edit Having seen your schema, it seems that you have not created a relationship between Content and Tags, which you need to do. You can let Doctrine handle their interaction. The Symfony and Doctrine book uses something essentially identical to your example to demonstrate how to do a many-to-many relationship. (Note that, although this document is for an out-of-date version of symfony, the syntax for this feature has not changed.)

How to export primary keys on data-dump?

When I export my database with doctrine:data-dump, I encounter 2 problems:
* the primary keys are not exported
* instead of foreign keys columns correct name, it uses the name of the foreign table.
For example, here are my tables:
# schema.yml
Planet:
connection: doctrine
tableName: planet
columns:
planet_id:
type: integer(4)
fixed: false
unsigned: false
primary: true
sequence: planet_planet_id
solarsystem_id:
type: integer(4)
fixed: false
unsigned: false
notnull: false
primary: false
# some columns...
relations:
Solarsystem:
local: solarsystem_id
foreign: solarsystem_id
type: one
# other relations...
Solarsystem:
connection: doctrine
tableName: solarsystem
columns:
solarsystem_id:
type: integer(4)
fixed: false
unsigned: false
primary: true
sequence: solarsystem_solarsystem_id
# other columns...
relations:
Planet:
local: solarsystem_id
foreign: solarsystem_id
type: many
# other relations
When I dump, I find things like that in data.yml:
Planet_1:
Solarsystem: _1
When I data-load that, it doesn't work (Invalid row key specified: (solarsystem) _1, referred to in (planet) Planet_1). I have to fix manually like this:
Planet_1:
solarsystem_id: 1
planet_id: 1
For the moment, I'm fixing the data.yml manually, but it begins to become a pain with all the records I'm accumulating...
Note: I'm using Symfony 1.4, Doctrine, postgreSQL, NetBeans, Windows. Feel free to ask information you would judge useful.
Thanks for your help
I recommend checking out this article entitled "Never Trust doctrine:data-dump": http://www.thomaskeller.biz/blog/2010/01/29/never-trust-doctrinedata-dump/
With that in mind, you may instead prefer checking out pg_dump:
http://www.postgresql.org/docs/8.1/static/backup.html#BACKUP-DUMP
This dump is postgreSQL level, and as such is not likely to care about -- or stumble over -- your Doctrine schema.

Resources