Symfony doctrine , unique records - symfony1

I use symfony 1.4.11 with doctrine.This is one of my tables:
Subscriptions:
connection: doctrine
tableName: subscriptions
columns:
user_id: { type: integer(4), primary: true }
category_id: { type: integer(4), primary: true }
relations:
sfGuardUser: { onDelete: CASCADE, local: user_id, foreign: id }
Categories: { onDelete: CASCADE, local: category_id, foreign: category_id }
I need to get all user_id from this table.
I make :
public function getSubscriptionsUser()
{
$q = $this->createQuery('a')
->select ('a.user_id');
return $q-> execute();
}
But if the user is subscribed to several categories, its id will be repeated several times. Is it possible to extract only unique id of user? If user have id = 1 , and it is repeated 10 times,in result I will have only "1" , but no "1 1 1 1 1 1 1 1 1 1" :-)
Thank you!

This should work out for you:
$q = $this->createQuery('a')
->select ('distinct(a.user_id) as user_id');

Related

How order doctrine query for many-to-many relationship?

i use doctrine 1.2 and here is my schema:
ProviderProduct:
columns:
provider_id:
type: integer
primary: true
product_id:
type: integer
primary: true
num:
type: integer
default: 0
unsigned: true
Provider:
columns:
name: {type: string(255), notnull: true, notblank: true, unique: true}
relations:
Products:
class: Product
local: provider_id
foreign: product_id
refClass: ProviderProduct
Product:
columns:
#a lot of columns
relations:
Providers:
class: Provider
local: product_id
foreign: provider_id
refClass: ProviderProduct
What i want is get product with max num (this is ammount) for specific provider.
I tried this query:
$query = Doctrine_Query::create()->select('p.*')
->from('Product p')
->innerJoin('p.Providers pr')
->where('pr.name = ?', 'WhiteStore')
->orderBy('ProviderProduct.num');
$query->fetchOne();
The result sql query:
SELECT p.* FROM product p
INNER JOIN provider_product p3 ON (p.id = p3.product_id)
INNER JOIN provider p2 ON p2.id = p3.provider_id, provider_product p4
WHERE (p2.name = ?) ORDER BY p4.num
As you can see it doesnt order by num field. So, what is the right dql for my task ?
How order doctrine query for many-to-many relationship?
You can use following construction:
$query = Doctrine_Query::create()->select('p.*')
->from('Product p')
->innerJoin('p.Providers pr')
->where('pr.name = ?', 'WhiteStore')
->orderBy('p.Providers.ProviderProduct.num');
The not native behavior of Doctrine 1 is creating an invisible alias for many-to-many relation. You can use this alias for building right order.
I think you need to relate refClass too:
ProviderProduct:
columns:
provider_id:
type: integer
primary: true
product_id:
type: integer
primary: true
num:
type: integer
default: 0
unsigned: true
relations:
Provider: { foreignAlias: ProviderProduct }
Product: { foreignAlias: ProviderProduct }
And then relate through reffClass to get the number:
$query = Doctrine_Query::create()->select('p.*')
->from('Product p')
->innerJoin('p.ProviderProduct pp')
->innerJoin('pp.Provider pr')
->where('pr.name = ?', 'WhiteStore')
->orderBy('pp.num DESC') // to get de max first
->groupBy('p.id') // if any duplicates
->limit(1);
$product = $query->fetchOne();

Adding multiple records to a relations table with symfony

I have setup 3 tables in symfony:
A flipbook table, A Skills table, and a relations table, to connect each skill id with each flipbook id.
WHen I built the model, symfony constructed everything correctly, and by default gave a drop-down menu for the skills, which had all skills from the skills table as options. You can select an option and it creates the appropriate relationships.
This is working (sort of). When you submit the form, it does not add the skill_id to the record. It just adds the auto-increment id to the skills relations table and neither the flipbook_id or skill_id. Aaaaaand, if you click more than one checkbox, you get this nice message:
invalid parameter number number of bound variables does not match number of tokens
Whaaaat does that mean? This has got to be a schema issue eh?
How about some code? yes.please.
Schema:
Flipbook:
tableName: flipbook
inheritance:
extends: SvaGeneric
type: concrete
columns:
title: { type: string(255) }
career_associations: { type: clob }
skills: { type: string(255) }
skills_associations: { type: clob }
program: { type: string(255) }
program_associations: { type: clob }
draft_id: { type: integer(10) }
FlipbookSkills:
tableName: flipbook_skills
columns:
title: { type: string(255) }
relations:
Flipbook:
foreignAlias: flipbook_skills
alias: skills
local: title
onDelete: SET NULL
FlipbookSkillRelations:
tableName: flipbook_skill_relations
actAs:
SoftDelete: ~
options:
columns:
flipbook_id: { type: integer(10), notnull: false }
skill_id: { type: integer(10), notnull: false }
relations:
Flipbook:
foreignAlias: flipbook_skills_flipbook
alias: flipbook
local: flipbook_id
onDelete: CASCADE
FlipbookSkills:
foreignAlias: flipbook_skills_skills
alias: flipbookskills
local: skill_id
onDelete: CASCADE
FlipbookSkillsRelationsForm.class.php:
$this->widgetSchema['flipbook_id'] = new sfWidgetFormInputText(array(), array('class' => 'text size-500'));
$this->widgetSchema['skill_id'] = new sfWidgetFormSelectCheckbox(array('choices' => "**WHAT GOES HERE??**"), array('class' => 'text size-500'));
$useFields = array(
'flipbook_id',
'skill_id',
);
$this->useFields($useFields);
Let me know if I should provide further code or explanation.
Here is the FlipbookSkillsTable.class generated in the model:
<?php
/**
* FlipbookSkillsTable
*
* This class has been auto-generated by the Doctrine ORM Framework
*/
class FlipbookSkillsTable extends Doctrine_Table
{
/**
* Returns an instance of this class.
*
* #return object FlipbookSkillsTable
*/
public static function getInstance()
{
return Doctrine_Core::getTable('FlipbookSkills');
}
public function retrieveForFilter()
{
$res = $this->createQuery('s')
->select('s.id, s.name')
->orderBy('s.name ASC')
->execute(array(), Doctrine_Core::HYDRATE_NONE);
// if you want an empty line
$rets = array('' => '');
foreach ($res as $ret)
{
$rets[$ret[0]] = $ret[1];
}
return $rets;
}
public function getAllFlipbookSkills() {
$allSkills = Doctrine_Query::create()
->from('FlipbookSkills')
->orderBy('title ASC')
->execute();
return $allSkills;
}
}
UPDATE
I believe the issue is it is trying to bind the multiple values in the same record. It should make a new record for each skill chosen, like this:
id flipbook_id skill_id
1 2 1
2 2 2
3 2 3
SO I still don't have the schema nailed down correctly, otherwise Doctrine would know how to handle the data correct? Should I explicitly put one to many relationship?
Also, when submitted, it is only adding the flipbook_id value to the record. The skill_id record is 0
FLipbookRelationsForm:
class FlipbookSkillRelationsForm extends BaseFlipbookSkillRelationsForm
{
public function configure()
{
$this->widgetSchema['skill_id'] =
new sfWidgetFormDoctrineChoice(array('model' =>
$this->getRelatedModelName('flipbookskills'),
'expanded' => true, 'multiple' => true, 'add_empty' => false));
$useFields = array(
'flipbook_id',
'skill_id',
);
$this->useFields($useFields);
}
}
Multiple issues (maybe this post can help: Embedded forms in symfony 1.4 not saving propperly)
Your n-m relations doesn't look well defined. Your entities should looks like:
FlipbookSkill:
tableName: flipbook_skill
columns:
title: { type: string(255) }
relations:
Flipbooks:
class: Flipbook
refClass: FlipbookSkillRelation
local: skill_id
foreign: flipbook_id
/* Not foreignAlias and onDelete behaviour defined */
Flipbook:
tableName: flipbook
inheritance:
extends: SvaGeneric
type: concrete
columns:
title: { type: string(255) }
career_associations: { type: clob }
skills: { type: string(255) } # why this????
skills_associations: { type: clob } # why this????
program: { type: string(255) }
program_associations: { type: clob }
draft_id: { type: integer(10) }
relations:
Skills:
class: FlipbookSkill
refClass: FlipbookSkillRelation
local: flipbook_id
foreign: skill_id
FlipbookSkillRelation:
tableName: flipbook_skill_relation
actAs:
SoftDelete: ~
options:
columns:
flipbook_id: { type: integer(10), notnull: false }
skill_id: { type: integer(10), notnull: false }
relations:
Flipbook:
foreignAlias: FlipbookSkills
local: flipbook_id
foreign: id
onDelete: CASCADE
FlipbookSkill:
foreignAlias: FlipbookSkills
local: skill_id
foreign: id
onDelete: CASCADE
Then, you methods/join allowed are:
$flipbookObject->getSkills(); Because flipbook entity n-m relation name "Skills", wich returns a FlipbookSkill DoctrineCollection. Or in query ('f.innerJoin Skills s')
$flipbookObject->getFlipbookSkills(); Because flipbookSkillRelation 1-n foreignAlias name, but probably you will never use this (because you don't really care about relations inself, instead you care about related skills).
... etc
If you have a well defined schema.yml, then your forms should work properly.
Plus:
why are you using and text input widget for an id field (flipbook_id) ????
And why are you using a sfWidgetFormSelectCheckbox if you are working with doctrine entities? You should use sfWidgetFormDoctrineChoice like the BaseEntity defines.
I hope this can help you.
Where are your validators ? Symfony forms don't work properly with missing validators or inappropriate ones. If your validator accept 1-1 relationship, and you send multiple values, it won't work.
Try this :
$this->validatorSchema->setOption('allow_extra_fields' , true);
$this->validatorSchema->setOption('filter_extra_fields' , false);
If your error disappear, then you have a problem with validators. Otherwise you can try to use a the sfEmbedded forms, you render an sfForm, and add multiple time your FlipbooSkillForm with selects. If i remeber well there is some tutorials showing how to add them in js.
http://nacho-martin.com/dynamic-embedded-forms-in-symfony

Symfony (Doctrine) : Fixtures one to many relation with custom fields

I'm looking to create fixtures file for the following schema (Movies library):
VodProgram:
actAs: { Timestampable: ~ }
columns:
title: { type: string(255), notnull: true }
year: { type: smallint }
...
relations:
VodPersons:
class: VodPerson
refClass: VodCasting
local: program_id
foreign: person_id
foreignAlias: VodPrograms
VodPerson:
columns:
name: { type: string(255), notnull: true }
VodCasting:
columns:
program_id: { type: integer, primary: true }
person_id: { type: integer, primary: true }
role: { type: string(255) }
rank: { type: smallint }
relations:
VodProgram: { onDelete: CASCADE, local: program_id, foreign: id }
VodPerson: { onDelete: CASCADE, local: person_id, foreign: id }
My problem is creating fixtures file with the "role" and "rank" fields present in the VodCasting table.
Here is my actual fixtures:
VodPerson:
kosinski:
name: Joseph Kosinski
VodProgram:
tron:
VodPersons: [kosinski] # where to put the role and rank infos ?
title: Tron
year: 2010
I tried something like :
VodProgram:
tron:
VodPersons:
kosinski:
rank: 1
title: Tron
...
with no luck.
Any suggestions?
Thanks.
Have you tried something like that :
VodPerson:
kosinski:
name: Joseph Kosinski
VodProgram:
tron:
title: Tron
year: 2010
VodCasting:
tron_kosinski:
role: 'Director'
rank: 1
VodPerson: kosinski
VodProgram: tron

Symfony sorted model with having request

I have two models : Operation and OperationHistory
Operation:
columns:
startdate: { type: timestamp, notnull: true }
enddate: { type: timestamp, notnull: true }
year: { type: integer, notnull: true }
abscomment: { type: string(500) }
person_id: { type: integer, notnull: true }
operationtype_id: { type: integer, notnull: true }
relations:
Person: { onDelete: CASCADE, local: person_id, foreign: id, foreignAlias: Operations}
OperationType: { onDelete: CASCADE, local: absencetype_id, foreign: id, foreignAlias: Operations }
OperationHistory:
columns:
oh_comment: { type: string(500), notnull: true }
oh_date: { type: timestamp, notnull: true }
status_id: { type: integer, notnull: true }
operation_id: { type: integer, notnull: true }
updater: { type: integer, notnull: true }
indexes:
date:
fields:
ohdate:
sorting: DESC
relations:
Operation: { onDelete: CASCADE, local: operation_id, foreign: id, foreignAlias: OperationsHistory }
Person: { onDelete: CASCADE, local: updater, foreign: id, foreignAlias: OperationsHistory }
OperationStatus: { onDelete: CASCADE, local: status_id, foreign: id, foreignAlias: OperationsHistory }
In order to get all Operation by Person, I use an index on OperationHistory. So if I need all the person's operation with a specific status:
public function getOperations($person, $status){
$q = $this->createQuery('o')
->leftJoin('o.OperationsHistory oh')
->leftJoin('p.Person p')
->groupBy('ah.absence_id')
->having('ah.status_id = ?', $status);
return $q->execute();
}
Thanks to the index on ohdate, I assume with the groupBy, I only get the last OperationHistory about a specific Operation. Without the having clause, It's good, but I only want Operations with a specific Status. But if I set this having clause, I get nothing at all.
In fact, I need to translate this sql request :
SELECT *
FROM operation o
LEFT JOIN (
SELECT *
FROM operation_history
ORDER BY ohdate DESC
) oh ON o.id = oh.absence_id
LEFT JOIN person p ON p.id = o.person_id
WHERE p.id = 1
GROUP BY oh.operation_id
HAVING oh.status_id = 1
sorry for my bad english and I hope these informations will be usefull to help me.
Thank u !
Its very difficult to understand what you are trying to do just based on your questions above - can you include some other information ?
You can use join and orderby methods of the Doctrine_Query class :
$query = Doctrine_Core::getTable('operation')->createQuery()
->innerjoin('operation_history ......')
->orderby('.....')
By using Doctrine_RawSql(), it works !
public function getAbsenceQueries($person, $status){
$q = new Doctrine_RawSql();
$q->select('{o.*}, {oh.*}')
->from('operation o LEFT JOIN (SELECT * from operation_history order by ohdate DESC) oh ON o.id=oh.operation_id LEFT JOIN person p ON p.id = o.person_id')
->where('mg.group_id = ?', $group)
->groupBy('ah.absence_id')
->having('ah.status_id = ?', $status)
->addComponent('o','Operation o')
->addComponent('oh','o.OperationsHistory oh')
->addComponent('p','o.Person p');
return $q->execute();
}

properties of a n:m relation in doctrine

Hy guys
I've got the following schema for my objects:
Product:
columns:
name: { type: string(255) }
Basket:
columns:
current_status: { type: integer }
relations:
Products: { class: Product, refClass: BasketProducts, onDelete: CASCADE }
BasketProducts:
columns:
product_id: { type: integer, primary: true }
basket_id: { type: integer, primary: true }
quantity: { type: integer(4) }
relations:
Product: { local: product_id, onDelete: CASCADE }
Basket: { local: basket_id, onDelete: CASCADE }
Now in the frontend I try to show the users basket, getting the products by
foreach($basket->getProducts() as $product) {
echo $product->getId();
echo $product->getName();
}
The question now, how can i access the quantity field from the BasketProducts?
You will need to query the middle table directly in order to do this.
A good way to do this, is to add a function in your Basket.class.php that will retrieve the data you need based on a BasketID.
You could also create the function in your BasketTable.class.php if you'd like to include the data when fetching a particular basket (ie. getBasketWithProductQuantities())
I don't have any Doctrine code handy at this time.

Resources