The database structure looks like
Most powerful features of Symfony2 is the system. A bundle is like a plugin in other software, but it have everything in it, like controllers , models, templates and resources to support some site features. In fact Symfony2 is itself a bundle. This gives you the flexibility to use pre-built features packaged in third-party bundles or to distribute your own bundles. It makes it easy to pick and choose which features to enable in your application and optimize them the way you want.
So it is important to separate functionality in bundles, the bundles structure looks like this:
Symfony2 contains a code generator module that allows you to generate the bundles
$ php app/console generate:bundle --namespace=NameSpace/MyNameBundle
You can also create the folders and files structure of bundles manually,and then register them to system, for that you should do this modification in app/AppKernel.php file:
// app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ..
new Blogger\BlogBundle\BloggerBlogBundle(),
);
// ..
return $bundles;
}
// ..
}
After you generate the bundles, it's necessary to generate the entities, you can also use Symfony2 generator or do it manually. For database interaction Doctrine2 bundle was used (it is included in standard package of Symfony2).Doctrine 2 is an object-relational mapper (ORM) for PHP 5.3.0+ that provides transparent persistence for PHP objects. It stays on top of a powerful database abstraction layer (DBAL). Object-Relational Mappers primary task is the transparent translation between (PHP) objects and relational database rows. The annotations have been used to tell Doctrine how to map and generate schemes.
First it's necessary to set database configuration, find parameters.ini and modify it to your database configuration:
// app/config/parameters.ini
[parameters]
database_driver = pdo_mysql
database_host = localhost
database_port =
database_name = xshare
database_user = db_user
database_password = db_password
Here is an example how to generate an entity
<?php
//src/Xshare/ProductBundle/Entity/Product.php
namespace Xshare\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
class Product
{
private $product_id;
private $name;
private $description;
private $image;
private $category;
private $user;
protected $requests;
protected $bookings;
private $total_nb = 0;
private $status = 0
private $enable = 1;
private $created_at;
private $updated_at;
public $file;
private $statistics = 0;
~~~~~
now you can generate a entity using Symfony 2 generator:
$ php app/console doctrine:generate:entities NameSpaceNameBundle:EntityName
in this case is XshareProductBundle:Product or create all entities, to do that just provide as argument the namespace of the bundle ex: XshareProductBundle;
this command will generate all needed functions to get and set entity proprieties
for more documentation access this link:
http://symfony.com/doc/current/book/doctrine.html
next step add some mapping information for doctrine using annotation
<?php
//src/Xshare/ProductBundle/Entity/Product.php
namespace Xshare\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/
* @ORM\Table(name="product", indexes = {@ORM\Index(name="search_idx", columns={"name", "created_at"})})
* @ORM\Entity(repositoryClass="Xshare\ProductBundle\Repository\ProductRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Product
{
/
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $product_id;
/**
* @ORM\Column(type="string")
*/
private $name;
/**
* @ORM\Column(type="text", length=1000)
*/
private $description;
/**
* @ORM\Column(type="string")
*/
private $image;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM\JoinColumn(name="category_id", referencedColumnName="category_id")
*/
private $category;
/**
* @ORM\ManyToOne(targetEntity="Xshare\UserBundle\Entity\User", inversedBy="products")
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id", onDelete="NO ACTION", onUpdate="NO ACTION")
*
*/
private $user;
/**
* @ORM\OneToMany(targetEntity="Requests", mappedBy="product")
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id", onDelete="CASCADE", onUpdate="NO ACTION")
*
*/
protected $requests;
/**
* @ORM\OneToMany(targetEntity="Booking", mappedBy="product")
*/
protected $bookings;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $total_nb = 0;
/**
* @ORM\Column(type="integer", length=1)
*/
private $status = 0;
/**
* @ORM\Column(type="boolean" )
*/
private $enable = 1;
/**
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @ORM\Column(type="datetime")
*/
private $updated_at;
public $file;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $statistics = 0;
~~~~~
Here you can find Annotations Reference
http://docs.doctrine-project.org/en/latest/reference/annotations-reference.html
also add some customized function if necessary
//src/Xshare/ProductBundle/Entity/Product.php
…
public function __construct()
{
$this->requests = new ArrayCollection();
$this->bookings = new ArrayCollection();
$this->setImage("no_photo.png");
$this->setCreatedAt(new \DateTime());
$this->setUpdatedAt(new \DateTime());
}
/**
* @ORM\preUpdate
*/
public function setUpdatedAtValue()
{
$this->setUpdatedAt(new \DateTime());
}
getRequests()
{
return $this->requests;
}
/**
* returns the name of the user
* @return type
*/
public function __toString()
{
return $this->getName();
}
/**
* returns the directory where the picture of the user is saved
* @return string
*/
public function getUploadDir()
{
return 'uploads/product';
}
/**
* returns the absolute path to the directory where the picture of the user is saved
* @return type
*/
public function getUploadRootDir()
{
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
/**
* returns the path to the user image
* @return type
*/
public function getWebPath()
{
return null === $this->image ? null : $this->getUploadDir().'/'.$this->image;
}
/**
* returns the absolute path to the user image
* @return type
*/
public function getAbsolutePath()
{
return null === $this->image ? null : $this->getUploadRootDir().'/'.$this->image;
}
/**
* performs some actions before the insert of the user
* @ORM\prePersist
*/
public function preUpload()
{
if (null !== $this->file) {
// do whatever you want to generate a unique name
$this->image = uniqid().'.'.$this->file->guessExtension();
}
}
/**
* performs some actions after the insert of the user
* @ORM\postPersist
*/
public function upload()
{
if (null === $this->file) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->file->move($this->getUploadRootDir(), $this->image);
unset($this->file);
}
/**
* performs some actions after the delete of the user
* @ORM\postRemove
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
help link:
http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
Next, the tables will be generated :
$ php app/console doctrine:scheme:create
Now if everything went well you should have empty tables in your database.
To test the entities you can use Doctrine Fixtures to populate the tables. In order to find out more details about that, access this link :
http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html