Monday, August 23, 2010

sugarbox part2

 autoload.php

$vendorDir = __DIR__.'/vendor';

require_once $vendorDir.'/symfony/src/Symfony/Framework/UniversalClassLoader.php';

use Symfony\Framework\UniversalClassLoader;

$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
    'Symfony'                    => $vendorDir.'/symfony/src',
    'Application'                => __DIR__,
    'Bundle'                     => __DIR__,
    'Doctrine\\Common'           => $vendorDir.'/doctrine-common/lib',
    'Doctrine\\DBAL\\Migrations' => $vendorDir.'/doctrine-migrations/lib',
    'Doctrine\\ODM\\MongoDB'     => $vendorDir.'/doctrine-mongodb/lib',
    'Doctrine\\DBAL'             => $vendorDir.'/doctrine-dbal/lib',
    'Doctrine'                   => $vendorDir.'/doctrine/lib',
    'Zend'                       => $vendorDir.'/zend/library',
));
$loader->registerPrefixes(array(
    'Swift_' => $vendorDir.'/swiftmailer/lib/classes',
    'Twig_'  => $vendorDir.'/twig/lib',
));
$loader->register();


Now we good to create class for our own application-blog. All the php files related to application goes to corresponding application bundles. In this case inside BlogBundle.

Inside BlogBundle you'll see these folders:
Controller
Entity
Resources
and this file BlogBundle.php
open BlogBundle.php

BlogBundle.php
 

namespace Application\BlogBundle;

use Symfony\Framework\Bundle\Bundle;

class BlogBundle extends Bundle
{
}

All your controller classess goes into Controller folder.
Model classess goes into Entity folder
All layouts, views, custom application routes goes into Resources folder.

Ok. let's begin with Model classess. In this tutorial we are using doctrine 2 as ORM. You can read more about doctrine 2 here. S2 also supports propel. So you have download the doctrine 2 from here.
In doctrine2 we can create schema from three files: plain php(annotation), YAML, xml. We go for annotation type, bcoz it has more advantage. In Entity folder create a php file – Blog.php

Blog.php
 

declare(ENCODING = 'utf-8');
namespace Application\BlogBundle\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints;

/**
 * @Entity(repositoryClass="Bundle\BlogBundle\Entity\BlogRepository")
 * @Table(name="posts")
 * @HasLifecycleCallbacks
 */


class Blog
{
    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('title', new Constraints\NotBlank());
        $metadata->addPropertyConstraint('title', new Constraints\MinLength(5));
        $metadata->addGetterConstraint('description', new Constraints\NotNull());
    }

        /**
         * @Id @Column(type="integer")
         * @GeneratedValue(strategy="AUTO")
         */
        private $id;
    /**
     *
     * @Column(type="string", length=250)
     *
     */
    protected $title;

    /**
     *
     *
     * @column(type="string", length=250)
     */
    protected $description;

  

    public function getId()
        {
            return $this->id;
        }

  
    public function setTitle($title) {
        $this->title = $title;
    }

  
    public function getTitle() {
        return $this->title;
    }

  
    public function setDescription($description) {
        $this->description = $description;
    }

  
    public function getDescription() {
        return $this->description;
    }

  

}

 Doctrine ORM change the above class into schema and create database. For this we use CLI(command line interface). To setup doctrine and configure our application we've to write blog/config/config.yml .

  let's take a look:

kernel.config:
    charset:       UTF-8
    error_handler: null

web.config:
    router:     { resource: "%kernel.root_dir%/config/routing.yml" }
    validation: { enabled: true, annotations: true }

web.templating:
    escaping:       htmlspecialchars


doctrine.dbal:
  connections:
    default:
      driverClass: Doctrine\DBAL\Driver\PDOMySql\Driver  #
      dbname:   todo
      user:     root
      password: admin
doctrine.orm: ~


in s2, you can create multiple database connection. here use single (default) connection. you can give the database name, mysql username, password .

[continue]
note: these tutorials are updated frequently. i know there are so many typos and errors are there. please comment those here. if you have any doubts disscuss here.

1 comment: