Using Symfony2, DQL and knplabs/knp-paginator-bundle – how to get around “Cannot count query which selects two FROM components, cannot make distinction”
Category: Symfony
Making an entity repository container aware in Symfony2.1
I had a need to add ACL rules to entities at the repository level.
My biggest struggle was getting dependancy injection working.
This is what I finally came up with after a fresh mind and an instant coffee:
Adding extra fields to FOSUserBundle / SonataUserBundle
Sadly, this isn’t really documented (at time of writing).
Adding new protected variables to your User.php entity will not actually create database entries when you try to do a doctrine:schema:update.
While one still needs to have the protected variables in this entity class, along with getters and setters, the actual creation work is within UserBundle/Resources/config/doctrine/User.orm.xml
Here is an example for adding a foreign key:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> <entity name="ApplicationSonataUserBundleEntityUser" table="fos_user_user"> <id name="id" column="id" type="integer"> <generator strategy="AUTO" /> </id> <many-to-one field="client" target-entity="MyvendorMyBundleEntityClient" orphan-removal=""> <join-columns> <join-column name="client_id" referenced-column-name="id" nullable="1"/> </join-columns> </many-to-one> </entity> </doctrine-mapping> |
Symfony 1.4 Doctrine 1.2 MS SQL Server
Web server: Linux (Ubuntu on my dev setup), Apache, PHP 5.3, Symfony 1.4, Doctrine 1.2.
Database server: Microsoft Windows 2008 Server, MS SQL Server
Trying to get Symfony to talk to the database server has been a painful experience for the last few days. But perseverance has paid off.
Lots of Googling with trial & error has resulted in actually achieving a development setup that will mirror the eventual production setup.
Short version:
- Follow the FreeTDS and ODBC setup instructions of http://jamesrossiter.wordpress.com/2011/03/08/connecting-to-microsoft-sql-server-using-odbc-from-ubuntu-server/
- Use the following in config/databases.yml
1 2 3 4 5 6 7 |
all: doctrine: class: sfDoctrineDatabase param: dsn: dblib:dbname=datasourcename;host=sqlserver; username: ### password: ### |
In the above snippit, replace ‘datasourcename’ with whatever you used in /etc/odbc.ini and replace ‘sqlserver’ with the name used in /etc/freetds/freetds.conf & /etc/odbc.ini
It’s late and I’ve been struggling to get this working for some time. I may expand this entry in the future if required.
References and insperation:
1) http://blog.acjacinto.com/2011/11/compiling-php-with-mssql-servers-native.html
2) http://www.microsoft.com/download/en/details.aspx?id=28160
3) http://jamesrossiter.wordpress.com/2011/03/08/connecting-to-microsoft-sql-server-using-odbc-from-ubuntu-server/
4) http://trac.symfony-project.org/wiki/HowToConnectToMSSQLServer
How to get sfFormExtraPlugin working in Symfony 1.4 using Doctrine
I’m talking about this plugin: http://www.symfony-project.org/plugins/sfFormExtraPlugin
For some reason, getting the plugin to work took a lot of Googling and some trial and error.
So here is how I got it working. Hopefully I did this in the ‘correct’ way…
1 2 3 4 5 6 7 8 9 10 11 12 13 |
cd plugins wget "http://plugins.symfony-project.org/get/sfFormExtraPlugin/sfFormExtraPlugin-1.1.3.tgz" tar zxvf sfFormExtraPlugin-1.1.3.tgz mv sfFormExtraPlugin-1.1.3 sfFormExtraPlugin cd .. ./symfony plugin:publish-assets cd web/js wget "http://code.jquery.com/jquery-1.4.3.min.js" wget "http://jqueryui.com/download/jquery-ui-1.8.5.custom.zip" mkdir jquery-ui cd jquery-ui unzip ../jquery-ui-1.8.5.custom.zip mv jquery-ui/css/smoothness ../css |
Edit config/ProjectConfiguration.class.php to add sfFormExtraPlugin as an enabled plugin:
1 2 3 4 5 6 7 |
class ProjectConfiguration extends sfProjectConfiguration { public function setup() { $this->enablePlugins(array('sfDoctrinePlugin', 'sfFormExtraPlugin')); } } |
Edit apps/<app_name>/config/view.yml to include the JS and CSS:
1 2 3 |
stylesheets: [main.css, smoothness/jquery-ui-1.8.5.custom.css] javascripts: [jquery-1.4.3.min.js, jquery-ui/js/jquery-ui-1.8.5.custom.min.js] |
Edit lib/form/doctrine/<your_module_name>Form.class.php to make use of the widgets you want. I’m interested in sfWidgetFormJQueryDate:
1 2 3 4 5 6 7 8 9 10 11 |
public function configure() { $this->widgetSchema['created_at'] = new sfWidgetFormJQueryDate(array( 'image' => '/images/silk_icons/calendar.png', 'config' => '{}', )); $this->widgetSchema['updated_at'] = new sfWidgetFormJQueryDate(array( 'image' => '/images/silk_icons/calendar.png', 'config' => '{}', )); } |
I have used the calendar.png icon from the famfamfam silk icon set. You can get the set here: http://www.famfamfam.com/lab/icons/silk/
I’ve used the date picker widget for filling in the created_at and updated_at fields. You can replace these values with the fields you need to manipulate in a user friendly way.
[Edit 17/10/2010:]
I’m not going mad, there is an issue with the ‘magic’ fields created_at and updated_at in that they have to be unset to become magic.
Found help here: http://levelx.me/technology/programming/symfony-1-4-doctrine-timestampable-behaviour/
Though the unset lines are better off being added just once to /lib/form/doctrine/BaseFormDoctrine.class.php, rather than each xxxForm.class.php file.