Oct 31
Digg
Stumbleupon
Technorati
Delicious

How to populate object_select_tag with your own objects

Sometimes you need to have an ability to show limited number of elements in your select objects or sort elements by name or so. I figured out that this feature was added to object_select_tag later so that not everybody knows about that. This feature’s name is ‘peer_method’ - effectively you create your peer method which returns objects which must appear in your object_select_tag. So here is the code for object_select_tag:

echo object_select_tag($domain, ‘getObjectId’, array (
‘related_class’ => ‘Object’,
‘peer_method’ => ‘getSortedObject’,
‘control_name’ => ‘object_id’,
‘include_blank’ => true,
));

and this must be put in your ObjectPeer.class.php:

static public function getSortedObject() {
$c = new Criteria();
$c->addAscendingOrderByColumn(TablePeer::NAME);
$rs = TablePeer::doSelect($c);
return $rs;
}

Interesting how this can be done in generator without re-writing of edit_form.php template. I suspect this can be done directly in generator.yml but never tried it. If someone has experienced would be good to know.

Thanks in advance.


Author: admin
Oct 30
Digg
Stumbleupon
Technorati
Delicious

Tricks with symfony .htaccess

I decided to put here a couple of .htaccess rules that may help you in your daily practice to save the time trying to get it working. Real .htaccess guru of course wont find it usefull but maybe someone will tell me thank you :-)

How to use Symfony on naked IP without domain name

Sometimes you dont have domain name but you would like to get your symfony project working. All you need is love, love, love and add the following line into your .htaccess

RewriteRule ^(.*)$ http://XX.XX.XX.XX/~accountname/index.php [QSA,L]

so full .htacces must look like this:

<IfModule mod_rewrite.c>
RewriteEngine On

# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /

# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]

# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteRule ^(.*)$ http://XX.XX.XX.XX/~accountname/index.php [QSA,L]
</IfModule>

How to get visitors redirected to WWW.

Sometimes you dont have domain name but you would like to get your symfony project working. All you need is love, love, love and add the following line into your .htaccess

RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

so full code must look like:

<IfModule mod_rewrite.c>
RewriteEngine On

# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]

# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

and the last but not least thing

How to use Symfony along with the other software in the same directory

This one is a bit more tricky that the other ones but also nothing complicated. I think you may found this or similar information on symfony forum but here I collected what I have been really tried and made sure it’s working. So lets consider a using of popular CMS Wordpress along with Symfony project. So because of WP uses index.php file as a core we would need to rename symfony’s index.php file into somthing else, lets name it symfony-index.php and next step it to add the following code into .htaccess:

RewriteRule ^/blog/wp-admin/(.*)$ /blog/wp-admin/index.php [L]
RewriteRule ^/blog/(.*)$ index.php [L]
RewriteRule . /index.php [L]
RewriteRule ^(.*)$ symfony-index.php [QSA,L]

Full .htaccess must look like this:

<IfModule mod_rewrite.c>
RewriteEngine On

# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /

# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]

# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

# no, so we redirect to our front web controller
RewriteRule ^/blog/wp-admin/(.*)$ /blog/wp-admin/index.php [L]
RewriteRule ^/blog/(.*)$ index.php [L]
RewriteRule . /index.php [L]
RewriteRule ^(.*)$ symfony-index.php [QSA,L]

</IfModule>


Author: admin
Oct 29
Digg
Stumbleupon
Technorati
Delicious

sfCaptcha documentation is updated

There were the major updates to sfCaptchaPlugin documentaation. Thanks a lot to crux_op for keeping this documentation page clean and up-to-dated.

I’d like to ask the community opinion on how this plugin should progress, i.e. which improvements you want to see in it.
What I’m thinking so far is about kind of skins for it, so it does not look so boring but it’s actually depends a lot on JpGraph as it’s the graphical library.


Author: admin
Oct 28
Digg
Stumbleupon
Technorati
Delicious

SimpleXML in Symfony

I thought I got crazy about it. I’ve discovered SimpleXML for myself and I felt in love with it.
I never though work with XML may become such simple and then… Then I thought I really hate SimpleXML coz it took out almost hour of my life.

So here is the situation I have a XML response like:

<elements>
<element>
<value1>XXX<value1>
</element>
<element/>
<value1>YYY</value1>
<element/>
</elements>

And I’m going to parse it and find out if there is element with value1 = XXX in my database..

So generally it looks like this:

$b = new sfWebBrowser();
$b->get(XML_SOURCE_URL);
$xmlstr = $b->getResponseText();
$xml = new SimpleXMLElement($xmlstr);
foreach ($elements as $e) {
$c = new Criteria();
$c->add(TablePeer::VALUE1, $e->value1);
$dom = TablePeer::doSelect($c);
}

It looks great and I was sure it MUST work great..
And what do you think I’ve got?

PHP Fatal error: Call to undefined method SimpleXMLElement::__toString() in C:\usr\php5\PEAR\pear\symfony\vendor\creole\common\PreparedStatementCommon.php on l
ine 596

Fatal error: Call to undefined method SimpleXMLElement::__toString() in C:\usr\php5\PEAR\pear\symfony\vendor\creole\common\PreparedStatementCommon.php on line 5
96

It’s fine. I’m quickly looking through symfony forum and find out this

where someones responses to someone:

you are probably trying to insert a simplexml objet into a string column.

i.e you are doing something like :
$this->feed = new SimpleXml($feedurl);
/*..*/
$this->save();

this can not work since simplexml has no toString method and you will have to do $this->feed->asXML()

But I’m not storing anything. Moreover, when I remove criteria stuff everything works fine, I can print out values but I can’t get connected to DB.

Well, after some time of investigating I figured out that correct code is:

$b = new sfWebBrowser();
$b->get(XML_SOURCE_URL);
$xmlstr = $b->getResponseText();
$xml = new SimpleXMLElement($xmlstr);
foreach ($elements as $e) {
$c = new Criteria();
$c->add(TablePeer::VALUE1, (string)$e->value1);
$dom = TablePeer::doSelect($c);
}

See the difference? Force type conversion to string. I’m disappointed.. I’m depressed.. I need some beer because I have not expected such bullshit may happen to me.

Have a great weekend guys!


Author: admin
Oct 26
Digg
Stumbleupon
Technorati
Delicious

Uml2Symfony

In previous post I wrote about Symfony2Exe stuff and now I’d like to bring a question of UML. I can’t say however how important is using of UML as I’ve never used it. Not because I dont know it but because the other dont know it. But as I figured out there is a tool which can be used to simply symfony development even if I’m the only person who is working on development process: Uml2Symfony

As it turns out from documentation you will need also ArgoUML software.

Here are the list of commands for Uml2Symfony:

uml2symfony-translate - which opens the design.uml file and generates schema files and plugins accordingly. A component in the UML diagram is translated into a symfony plugin, and the classes inside the component is added to the plugin schema file.

uml2symfony-crud-plugins - Which creates modules for each class in a component. Warning! This task does not yet test whether you have made any changes to the actions or templates, it overwrites what ever is in the plugin modules dir.

uml2symfony-load - Is a wrapper for propel-load-data. If you have set up databases.yml and propel.ini correctly but still get the error message: ”’No connection params set for propel”’ you can call this task which initializes propel correctly, adds lib and plugins/libs to autoload and then calls propel-load-data.

uml2symfony-all - Is not quite all :-). It calls uml2symfony-translate, propel-build-model, propel-build-sql, propel-insert-sql, uml2symfony-load, but excludes uml2symfony-crud-plugins.

It’s wonderfull! I guess it’s worth to start using ArgoUML and then e.g. generate plugins automatically. I’d say it’s good step towards Symfony Rapid Environment. Btw, I’m thinking about that these weeks. How about to integrate symfony with Uml2Symfony and WApache and have also some tools to draw quick pages layout (like Axure Pro has). As for me it should not be complicated to build up something like that on Delphi. And I’m sure that’s what Symfony community will really appreciate.

Makes sense?


Author: admin
Oct 25
Digg
Stumbleupon
Technorati
Delicious

Symfony2Exe

I have just read about an ability to convert php applications into exe with Wapache project: How to turn a php script to an exe..for free

As it’s written in post:

This could be used for a demo/trial of a web application. A windows installer could also be used (NSIS works well and is free) to create a fully installable, desktop application.

And I really like it!

However when I’ve been trying to run it up I figured out that still use Apache 1.3 and actually dont see any reason to upgrade to 2.0 :-)
WApache requires Apache 2 to start up. Techinally I think that Symfony-based applications must be also converted into exe with WApache but never knows before try you it. If anyone would be able to try it out and let us know about the results it would be really great. So guys if you’ll have a chance please drop us a line ;-) We promise to publish the results right here. We may even put here your converted app as a demo and I guess that can be good advertise for you.

On the other hand, if WApache can’t convert symfony projects to exe. Has anyone tried to do something like that. Maybe Symfony2Exe script or so :-)
Or how about pake task for symfony which converts symony project with/without wapache into windows binary… Oh you see I have too much ideas :-)


Author: admin
Oct 25
Digg
Stumbleupon
Technorati
Delicious

sfSettingsPlugin 1.0.1 has been released

New version of sfSettingsPlugin is added to Symfony repository:

http://trac.symfony-project.com/wiki/sfSettingsPlugin

It contains usefull fix to clean out cached file with settings from both frontend and backend environments in production mode.
This way user does not have manually clean out cache when he did a change in backend for settings values.

However there is still a small problem which is going to be fixed soon:

right now when user changes settings values from backend cached file config_db_settings.yml.php is deleting in frontend and backend environments:

$cache_file = sfConfig::get(’sf_root_dir’).’/cache/frontend/prod/config/config_db_settings.yml.php’; sfToolkit::clearGlob($cache_file);

but if you use another environment names you have to go in code actions.class.php and fix it manually.


Author: admin
Oct 24
Digg
Stumbleupon
Technorati
Delicious

sfCaptchaPlugin 1.0.4 has been released

New version of sfCaptchaPlugin is added to Symfony repository:

http://trac.symfony-project.com/wiki/sfCaptchaPlugin

It contains small but quite important fix to remove zero from captcha alphabet as it does not fit newer JpGraph version as was reported by some developers.

By reviewing the code I noticed that there is used dynamical route for sf_captcha and I remembered that this other developer told me that dynamical routes are not really good solution - as they are not caching. So if you want to speed up captcha appereance you’ll have to remove dynamical route and simply add it to your main routing.yml. Of course, this wont have much effect on small sites but for heavy-loaded ones this can be quite helpfull. By the way, the same situation is for sfGuardPlugin and the other ones symfony native plugins.


Author: admin
Oct 22
Digg
Stumbleupon
Technorati
Delicious

sfCaptcha fix

I’ve got an email regard to sfCaptcha plugin. I think this can be interesting to everyone who uses this plugin so I decided to publish it here. I’m going also to fix code the way described here but until then this is good advise for everyone who uses my captch solution and sometimes got a problem with captcha code appereance:

Hello,

We are using your sfCaptcha plugin and noticed the following:

In the 1.02 version of the sfCaptcha plugin in Captcha.class.php the
seed starts with 0 (zero):

public function generate() {
$this->iData = ”;
$alphabet = sfConfig::get(’app_captcha_alphabet’, ‘0123456789′);

Nevertheless in jpgraph 2.2 in jpgraph_antispam.php, there is a
validation that returns false if the char is 0 or o:

function Stroke() {
$n=strlen($this->iData);
if( $n==0 ) {
return false;
}
for($i=0; $i < $n; ++$i ) {
if( $this->iData[$i]===’0′ || strtolower($this->iData[$i])===’o') {
return false;
}
}

This of course, causes random blank images in our application. Our
suggestion fix is to start the default seed from 1 like so:

$alphabet = sfConfig::get(’app_captcha_alphabet’, ‘123456789′);

It’s working perfectly for us this way.


Author: admin
Oct 22
Digg
Stumbleupon
Technorati
Delicious

Swift mailer

I would like to drop here a couple of lines about Swift mailer.
If you are going to send message to the big number of email recepients it’s obviously you’ll need to write a batch script so it processes this in background. And it would be good to have something (mailer layer) that you can rely one and use for sending out tons of messages, newsletters or maybe even evil spam, why not.

So I’ve looked through Symfony forum and found a couple of posts which say to try SwiftMailer as a solution. And what is also important - there is already done swift bridge plugin for it: sfSwiftPlugin.

Swift is a fully OOP library for sending e-mails from PHP websites and applications. It does not rely on PHP’s native mail() function which is known for using high server resources when sending multiple emails. Instead, Swift communicates directly with an SMTP server or a MTA binary to send mail quickly and efficiently.

Early versions of Swift were comparable to PHPMailer. Swift has since evolved and matured into a fully-fledged object-oriented mailing solution. Compared with PHPMailer, the interface for Swift is both tighter and more intuitive. I genuinely belive that no other mailer comes close to Swift in terms of available features.

Swift supports event-driven plugins which offer you the opportunity to really take control of the library and set this mailer apart from everything else that’s out there. Over time, and from previous versions, the library has been steadily refactored and is now usable for more than just the blind sending of emails. It can compose RFC 2822 compliant messages for use elsewhere too. I’m looking at offering the reverse and parsing real emails into the message object format used in the library.

I refer you to swift doc so you can get more ideas how to built a site with it and which features it has http://www.swiftmailer.org/wikidocs/

Ok, so all you need to start it up is to add these lines to your batch script:

$swift =& new Swift(new Swift_Connection_SMTP(smtp_server_address));
$recipients =& new Swift_RecipientList();
$message =& new Swift_Message($email_subject, $email_body);
foreach ($addresses as $address) $recipients->addTo($address);
$swift->batchSend($message, $recipients, $from_email_address);

and go ahead with it! Interesting to know if someone has ever used Swift in production and what is performance and how it works for big amount of addresses. I can’t find such info on web.


Author: admin