Blog ب symfony 4 الجزء الأول
نظرة سريعة بالفيديو
1- إضافة قاعدة البيانات
composer require symfony/orm-pack composer require --dev symfony/maker-bundle
//.env file
//update this line
DATABASE_URL=mysql://root:''@127.0.0.1:3306/sym-blog
2- إضافة post table
php bin/console doctrine:database:create
php bin/console make:migration
//Post.php
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Post
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $body;
/**
* @ORM\Column(type="datetime")
*/
private $time;
/**
* @ORM\Column(type="string", length=255)
*/
private $slug;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User",inversedBy="posts")
* @ORM\JoinColumn()
*/
private $user;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Comment",mappedBy="post")
* @ORM\OrderBy({"created" = "DESC"})
*/
private $comments;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\User",inversedBy="postsLiked")
* @ORM\JoinTable(name="post_likes",
* joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
* )
*/
private $likedBy;
public function __construct(){
$this->likedBy = new ArrayCollection();
$this->comments = new ArrayCollection();
}
/**
*@return Collection
*/
public function getLikedBy(){
return $this->likedBy;
}
/**
*@return Collection
*/
public function getComments(){
return $this->comments;
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getBody(): ?string
{
return $this->body;
}
public function setBody(string $body): self
{
$this->body = $body;
return $this;
}
public function getTime(): ?\DateTimeInterface
{
return $this->time;
}
public function setTime(\DateTimeInterface $time): self
{
$this->time = $time;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getUser()
{
return $this->user;
}
public function setUser($user): self
{
$this->user = $user;
return $this;
}
//set time evry time we add a record
/**
* @ORM\PrePersist
*/
public function setTimeValue()
{
$this->time = new \DateTime();
}
//check if current user already like a post
public function like(User $user){
if($this->getLikedBy()->contains($user)){
return false;
}
$this->getLikedBy()->add($user);
}
}
3- إضافة ل migration
//
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20190311122335 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('CREATE TABLE post (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) NOT NULL, body LONGTEXT NOT NULL,slug VARCHAR(255) NOT NULL, time DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('DROP TABLE post');
}
}
4- إضافة des posts عشوائيين فقاعدة البيانات
منبعد غادي تمشي لقاعدة البيانات غادي تلقى table post تزادت.
//
<?php
namespace App\DataFixtures;
use App\Entity\Post;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Cocur\Slugify\Slugify;
class PostFixture extends Fixture
{
public function load(ObjectManager $manager)
{
$slugify = new Slugify();
for($i=0;$i<10;$i++){
$post = new Post();
$post->setTitle('Lorem ipsum post number '.rand(0,100));
$post->setBody('Lorem ipsum dolor sit amet consectetur adipisicing elit. Aperiam earum et quasi illo quidem dicta, officia ipsam molestiae nesciunt nemo vero voluptatibus autem necessitatibus odit expedita exercitationem sunt, nostrum magnam? '.rand(0,100));
$post->setTime(new \DateTime());
$post->setSlug($slugify->slugify($post->getTitle()));
$manager->persist($post);
}
$manager->flush();
}
}
5- إضافة des posts عشوائيين فقاعدة البيانات تتمة
//
php bin/console doctrine:fixtures:load