http://www.symfony-project.org/reference/1_4/en/07-Databases
symfony use databases.yml to determine the connection settings (host, database name, user, and password)
# ProjectName/config/database.yml all: xxxsession:
class: sfPropelDatabase
param:
dsn: mysql://xxxxxxx
Problem:
Fatal error: Class 'sfPropelDatabase' not found in /root/workspace/ProjectName/cache/appName/dev/config/config_databases.yml.php on line 6
From doctrine to Prople
http://stackoverflow.com/questions/1835676/switch-symfony-1-4-from-doctrene-to-propel
#ProjectName/config/ProjectConfiguration.class.php public function setup()
{
// $this->enablePlugins('sfDoctrinePlugin');
$this->enablePlugins('sfPropelPlugin');
}
Problem:
Fatal error: Declaration of MysqlSession::sessionWrite() must be compatible with that of sfDatabaseSessionStorage::sessionWrite() in /root/workspace/ProjectName/lib/symfony/MysqlSession.class.php on line 2
#ProjectName/lib/symfony/MysqlSession.class.php
public function sessionWrite($id, &$data) //reference parameter
Problem:
Unable to open PDO connection [wrapped: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)]
3. Incomplete Object Fatal error: xxx_xxxActions::executeGroup() : The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition XXXXXXX of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in /root/workspace/xxxx/actions/actions.class.php on line 40
Add the XXXXXXX object into the config.php
require_once('xxxxxx/service/xxxx/XXXXXXX.php');
4. Symfony cc
Different environment, index_dev.php works, index.php not.
Try to symfony10 cc, command not found;
Use ./symfony cc
Use YAHOO.lang.extend
superclass is part of the inheritance mechanism provided by YUI’s extend method. After you declare the constructor for your new object, you call extend:
YAHOO.lang.extend(YAHOO.SATYAM.LoadingPanel, YAHOO.widget.Panel);
The constructor itself does not get executed immediately. The extend function does get executed immediately.
We use method call of JavaScript native Function object, which takes the first argument as the execution scope of the function called and passes it the rest of the arguments.
To Use the object we just created
if (!loadingPanel2) {
loadingPanel2 = new YAHOO.SATYAM.LoadingPanel();
}
loadingPanel2.show();
Puts all the code within an anonymous function, which gets immediately executed (notice the empty parenthesis at the end)
YAHOO.namespace('SATYAM');
(function(){
var Dom = YAHOO.util.Dom,
Event = YAHOO.util.Event, Panel = YAHOO.widget.Panel;
// here goes the library contents itself
// constructor
// extend component
})();
YAHOO.register('SATYAM.LoadingPanel', YAHOO.SATYAM.LoadingPanel, {version: "0.99", build: '11'});
After we declare the instance of the YUI Loader that we will use, we call method addModule providing the information about our library file
var loader = new YAHOO.util.YUILoader();
YUI 2: Drag & Drop http://developer.yahoo.com/yui/dragdrop/
var dd1 = new YAHOO.util.DD("element1");
var dd2 = new YAHOO.util.DDProxy("element2"); //proxy element during movement
var dd3 = new YAHOO.util.DDTarget("element3"); //target for drag & drop interaction
Drag & Drop Manager
YAHOO.util.DragDropMgr (or alias YAHOO.util.DDM)
//sets the time delay between a mousedown and a
//drag event to 1200 milliseconds:
YAHOO.util.DragDropMgr.clickTimeThresh = 1200;
Interaction Groups
Each Drag and Drop object belongs to one or more interaction groups. Drag and Drop events fire only when the dragged element interacts with other objects that with which it shares a group membership. If no group is specified in the constructor, an object belongs to the "default" group
// No group specified so dd0 is assigned to the "default" group
var dd0 = new YAHOO.util.DD("elementid0");
// dd1 is a member of group1
var dd1 = new YAHOO.util.DD("elementid1", "group1");
// dd2 is a member of group2
var dd2 = new YAHOO.util.DD("elementid2", "group2");
// dd3 is a member of both group1 and group2
var dd3 = new YAHOO.util.DD("elementid3", "group1");
dd3.addToGroup("group2");
//groups can be removed via script as well:
dd1.removeFromGroup("group1");
Interesting Moments & Custom Events
var dd = new YAHOO.util.DD('demo');
dd.on('dragEvent', function(ev) {
YAHOO.log('Element is being dragged.');
}, dd, true);
assign a variable to an unnamed function: consider the function as an object
var add = function(a, b)
{
return a+b;
}
var add=function theAdd(a, b)
{
return a+b;
}
alert(add(1,2)); // produces 3
alert(theAdd(1,2)); // also produces 3
Useful in object oriented program, we can have a function be a property of an object
var myObject=new Object();
myObject.add=function(a,b){return a+b};
// myObject now has a property/a method named "add"
// and I can use it like below
myObject.add(1, 2);
When we declare a function, JavaScript actually creates an object;
We can add properties to Objects, including function objects.
function Ball() // it may seem odd, but declaration
{ // creates an object named Ball, and you can
} // refer to it or add properties to it like below
Ball.callsign="The Ball"; // add property to Ball
alert(Ball.callsign); // produces "The Ball"
Since function is an object, we can assign a pointer to a function
function myFunction(message)
{
alert(message);
}
var ptr=myFunction; // ptr points to myFunction
ptr("hello"); // executes myFunction which will prints "hello"
By declaring a function, we have also created a new data type
function Ball(message)
{
alert(message);
} var ball0=new Ball("creating new Ball"); // creates object &
// prints the message
ball0.name="ball-0"; // ball0 now has a "name" property
alert(ball0.name); // prints "ball-0"
The red portion as a shortcut for doing below
function Ball(message)
{
alert(message);
} var ball0=new Object(); ball0.construct=Ball; ball0.construct("creating new ball"); // executes ball0.Ball("creating..");
ball0.name="ball-0";
alert(ball0.name);
Constructor function
function Ball(message, specifiedName)
{
alert(message);
this.name=specifiedName;
}
var ball0=new Ball("creating new Ball", "Soccer Ball");
alert(ball0.name); // prints "Soccer Ball"
The "new" keyword eventually causes the constructor function to be executed. In this case, it will executel Ball("creating new Ball", "Soccer Ball"); and the
keyword this will refer to ball0.
Therefore, the line: this.name=specifiedName becomes ball0.name="Soccer Ball".
Every constructor function has a property named prototype.
You do not need to explicitly declare a prototype property, because it exists on every constructor function.
function Test()
{
}
alert(Test.prototype); // prints "Object"
Prototype is an object
when an object is created, the constructor function assigns its prototype property to the internal __proto__ property of the new object.
function Fish(name, color)
{
this.name=name;
this.color=color;
}
Fish.prototype.livesIn="water";
Fish.prototype.price=20;
You can use prototype to assign functions that are common on all objects
function Employee(name, salary)
{
this.name=name;
this.salary=salary;
}
Employee.prototype.getSalary=function getSalaryFunction()
{
return this.salary;
}
Employee.prototype.addSalary=function addSalaryFunction(addition)
{
this.salary=this.salary+addition;
}
$t = new lime_test(1);
$t->pass('This test always passes.');
To launch the test $ php test/unit/test.php
Sample
require_once dirname(__FILE__).'/../bootstrap/unit.php';
$t = new lime_test(6); // display 1..6
$t->comment('::slugify()'); // display #::slugify()
$t->is(Jobeet::slugify('Sensio'), 'sensio', '::slugify() converts all characters to lower case'); // display ok 1 - ::slugify() converts all ...
3. User Flash (symfony 1.4) http://www.symfony-project.org/jobeet/1_4/Doctrine/en/13
A flash is an ephemeral message stored in the user session that will be automatically deleted after the very next request. It is very useful when you need to display a message to the user after a redirect.
setFlash(): The first argument is the identifier of the flash and the second one is the message to display. You can define whatever flashes you want, but notice and error are two of the more common ones.
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend(sfWebRequest $request)
{
$request->checkCSRFProtection();
$this->getUser()->setFlash('notice', sprintf('Your job validity has been extended until %s.', $job->getDateTimeObject('expires_at')->format('m/d/Y')));
4. User Attribute (symfony 1.4) http://www.symfony-project.org/jobeet/1_4/Doctrine/en/13 getAttribute(), setAttribute()
// apps/frontend/modules/job/actions/actions.class.php
class jobActions extends sfActions
{
public function executeShow(sfWebRequest $request)
{
$this->job = $this->getRoute()->getObject();
// fetch jobs already stored in the job history
$jobs = $this->getUser()->getAttribute('job_history', array());
// add the current job at the beginning of the array
array_unshift($jobs, $this->job->getId());
// store the new job history back into the session
$this->getUser()->setAttribute('job_history', $jobs);
}
} myUser class
The myUser class overrides the default symfony base sfUser class with application specific behaviors
// apps/frontend/modules/job/actions/actions.class.php
class jobActions extends sfActions
{ public function executeShow(sfWebRequest $request) { $this->job = $this->getRoute()->getObject();
$this->getUser()->addJobToHistory($this->job); }
}
// apps/frontend/lib/myUser.class.php
class myUser extends sfBasicSecurityUser
{ public function addJobToHistory(JobeetJob $job) { $ids = $this->getAttribute('job_history', array());
if (!in_array($job->getId(), $ids)) { array_unshift($ids, $job->getId()); $this->setAttribute('job_history', array_slice($ids, 0, 3)); }
}
} remove attribute
User's attributes are managed by an object of class sfParameterHolder. The getAttribute() and setAttribute() methods are proxy methods for getParameterHolder()->get() and getParameterHolder()->set().
// apps/frontend/lib/myUser.class.php
class myUser extends sfBasicSecurityUser
{ public function resetJobHistory() { $this->getAttributeHolder()->remove('job_history'); }
}
5. i18n and l10n (symfony 1.4) http://www.symfony-project.org/jobeet/1_4/Doctrine/en/19 User Culture
The language French is ‘fr’, English is ‘en’; the country Canada is ‘CA’, United States is ‘US’; the culture for a user speaking French from Canada is ‘fr_CA’
setCulture()and getCulture()
// in an action
$this->getUser()->setCulture('fr_BE');
echo $this->getUser()->getCulture();
The preferred user culture can be configured in the settings.yml configuration file
#apps/frontend/config/settings.yml
all:
.settings:
default_culture: en_CA
Internationalization
set i18n setting to true in settings.yml
# apps/frontend/config/settings.yml
all:
.settings:
charset: utf-8
i18n: true
The I18N helper group is not loaded by default, you need to either manually add itin each template with use_helper(‘I18N’) or load it globally by adding to the standard_helpers setting
# apps/frontend/config/settings.yml
all:
.settings:
standard_helpers: [Partial, Cache, I18N]
Each translation is managed by a trans-unit tag which has a unique id attribute. A lot of tools exist to ease the translation process. (check out Open Language Tools https://open-language-tools.dev.java.net/)
7. Authentication (symfony 1.4)
entire application require the user to be authenticated
# apps/backend/config/security.yml
default:
is_secure: true
when an un-authenticated user tries to access a secured action, symfony forwards the request to the login action
# apps/backend/config/setting.yml
all: .actions:
login_module: default
login_action: login
By default, the myUser class extends sfBasicSecurityUser, and not sfUser. sfBasicSecurityUser provides additional methods to manage user authentication and authorization.
To manage user authentication, use the isAuthenticated() and setAuthenticated() methods:
if (!$this->getUser()->isAuthenticated())
{
$this->getUser()->setAuthenticated(true);
}
9. Use Helper in Action http://snippets.symfony-project.org/snippet/69
In PrjectConfiguration file
# ProjectName/config/ProjectConfiguration.class.php
include_once('/opt/symfony/symfony14/lib/helper/DateHelper.php');