Post Detail

Post #194

ICT-150 Doc

Documentation

Linux

Commands

mkdir <name> create folder
rmdir <name> delete folder
ls list all files
ls -al list all files even hidden and folders
clear clear CLI, keeps commands history

Git

Commands

git --help help
git init initialize repo
git clone clone repo
git log show git history
git log --all --oneline -- foo.bar finds all the commits with changes on this file
git log --all --oneline -p -- foo.bar like above but prints history

Some more in-depth help

Git the simple guide

Doctrine

Help

vendor\bin\doctrine orm:schema-tool:update --dump-sql --force
vendor\bin\doctrine orm:validate-schema

  1<?php
  2# src/Entity/User.php
  3
  4namespace Tuto\Entity;
  5
  6use Doctrine\Common\Collections\ArrayCollection;
  7use Doctrine\ORM\Mapping as ORM;
  8
  9/**
 10 * @ORM\Entity
 11 * @ORM\Table(
 12 *       name="users",
 13 *       indexes={
 14 *           @ORM\Index(name="search_firstname_lastname", columns={"firstname", "lastname"}),
 15 *           @ORM\Index(name="search_role", columns={"role"})
 16 *       }
 17 *   )
 18 */
 19class User
 20{
 21    /**
 22     * @ORM\Id
 23     * @ORM\GeneratedValue)
 24     * @ORM\Column(type="integer")
 25     */
 26    protected $id;
 27    /**
 28     * @ORM\Column(type="string")
 29     */
 30    protected $firstname;
 31
 32    /**
 33     * @ORM\Column(type="string")
 34     */
 35    protected $lastname;
 36
 37    /**
 38     * @ORM\Column(type="string")
 39     */
 40    protected $role;
 41
 42    /**
 43     * @ORM\ManyToMany(targetEntity=Poll::class)
 44     */
 45    protected $polls;
 46
 47    /**
 48     * @ORM\OneToMany(targetEntity=Participation::class, mappedBy="user")
 49     */
 50    protected $participations;
 51
 52    /**
 53     * @ORM\OneToOne(targetEntity=Address::class, cascade={"persist", "remove"}, inversedBy="user")
 54     */
 55    protected $address;
 56
 57    public function __construct()
 58    {
 59        $this->polls = new ArrayCollection();
 60        $this->participations = new ArrayCollection();
 61    }
 62
 63    public function getId()
 64    {
 65        return $this->id;
 66    }
 67
 68    public function setId($id)
 69    {
 70        $this->id = $id;
 71    }
 72
 73    public function getFirstname()
 74    {
 75        return $this->firstname;
 76    }
 77
 78    public function setFirstname($firstname)
 79    {
 80        $this->firstname = $firstname;
 81    }
 82
 83    public function getLastname()
 84    {
 85        return $this->lastname;
 86    }
 87
 88    public function setLastname($lastname)
 89    {
 90        $this->lastname = $lastname;
 91    }
 92
 93    public function getRole()
 94    {
 95        return $this->role;
 96    }
 97
 98    public function setRole($role)
 99    {
100        $this->role = $role;
101    }
102
103    /**
104     * @return mixed
105     */
106    public function getAddress()
107    {
108        return $this->address;
109    }
110
111    /**
112     * @param mixed $address
113     */
114    public function setAddress($address): void
115    {
116        $this->address = $address;
117    }
118
119    public function __toString()
120    {
121        $format = "User (id: %s, firstname: %s, lastname: %s, role: %s, address: %s)\n";
122        return sprintf($format, $this->id, $this->firstname, $this->lastname, $this->role, $this->address);
123    }
124
125    /**
126     * @return ArrayCollection
127     */
128    public function getPolls(): ArrayCollection
129    {
130        return $this->polls;
131    }
132
133    /**
134     * @param ArrayCollection $polls
135     */
136    public function setPolls(ArrayCollection $polls): void
137    {
138        $this->polls = $polls;
139    }
140
141    /**
142     * @return ArrayCollection
143     */
144    public function getParticipations()
145    {
146        return $this->participations;
147    }
148
149    /**
150     * @param ArrayCollection $participations
151     */
152    public function setParticipations(ArrayCollection $participations): void
153    {
154        $this->participations = $participations;
155    }
156}
 1<?php
 2# create-user.php
 3
 4$entityManager = require_once join(DIRECTORY_SEPARATOR, [__DIR__, 'bootstrap.php']);
 5
 6use Tuto\Entity\User;
 7
 8$admin = new User();
 9$admin->setFirstname("First ");
10$admin->setLastname("LAST ");
11$admin->setRole("admin");
12$entityManager->persist($admin);
13echo $admin . "</br>";
14
15foreach (range(1, 10) as $index) {
16    $user = new User();
17    $user->setFirstname("First ".$index);
18    $user->setLastname("LAST ".$index);
19    $user->setRole("user");
20    $entityManager->persist($user);
21    echo $user . "</br>";
22}
23
24$entityManager->flush();
 1<?php
 2# get-user.php
 3
 4$entityManager = require_once join(DIRECTORY_SEPARATOR, [__DIR__, 'bootstrap.php']);
 5
 6use Tuto\Entity\User;
 7
 8$userRepo = $entityManager->getRepository(User::class);
 9
10$user = $userRepo->find(1);
11echo "User by primary key: </br>";
12echo $user . "</br>";
13
14$allUsers = $userRepo->findAll();
15echo "</br>All users: </br>";
16foreach ($allUsers as $user) {
17    echo $user . "</br>";
18}
19
20$usersByRole = $userRepo->findBy(["role" => "admin"]);
21echo "</br>Users by role: </br>";
22foreach ($usersByRole as $user) {
23    echo $user . "</br>";
24}
25
26$usersByRoleAndFirstname = $userRepo->findBy(["role" => "user", "firstname" => "First 2"]);
27echo "</br>Users by role and firstname: </br>";
28foreach ($usersByRoleAndFirstname as $user) {
29    echo $user . "</br>";
30}
31
32$limit = 4;
33$offset = 2;
34$orderBy = ["firstname" => "DESC"];
35$usersByRoleWithFilters = $userRepo->findBy(["role" => "user"], $orderBy, $limit, $offset);
36echo "</br>Users by role with filters: </br>";
37foreach ($usersByRoleWithFilters as $user) {
38    echo $user . "</br>";
39}
40
41//$usersByRole = $userRepo->findBy(["role" => "admin"]);
42$usersByRole = $userRepo->findByRole("admin");
43echo "</br>Users by role: </br>";
44foreach ($usersByRole as $user) {
45    echo $user . "</br>";
46}
 1<?php
 2# update-user.php
 3
 4$entityManager = require_once join(DIRECTORY_SEPARATOR, [__DIR__, 'bootstrap.php']);
 5
 6use Tuto\Entity\User;
 7
 8$identifiant = 11;
 9
10$userRepo = $entityManager->getRepository(User::class);
11
12// Récupération de l'utilisateur (donc automatiquement géré par Doctrine)
13$user = $userRepo->find($identifiant);
14
15$user->setFirstname("First Real Modification");
16$user->setLastname("Last Real Modification");
17
18$entityManager->flush();
 1<?php
 2# delete-user.php
 3
 4$entityManager = require_once join(DIRECTORY_SEPARATOR, [__DIR__, 'bootstrap.php']);
 5
 6use Tuto\Entity\User;
 7
 8$identifiant = 2;
 9
10$userRepo = $entityManager->getRepository(User::class);
11
12// Récupération de l'utilisateur (donc automatiquement géré par Doctrine)
13$user = $userRepo->find($identifiant);
14
15$entityManager->remove($user);
16$entityManager->flush($user);
17
18// Récupération pour vérifier la suppression effective de l'utilisateur
19$user = $userRepo->find($identifiant);
20
21var_dump($user); // doit renvoyer NULL

composer require doctrine/orm:^2.8.*

Create a file bootstrap.php

 1<?php
 2# bootstrap.php
 3
 4require_once join(DIRECTORY_SEPARATOR, [__DIR__, 'vendor', 'autoload.php']);
 5
 6use Doctrine\ORM\Tools\Setup;
 7use Doctrine\ORM\EntityManager;
 8
 9$entitiesPath = [
10    join(DIRECTORY_SEPARATOR, [__DIR__, "src", "Entity"])
11];
12
13$isDevMode = true;
14$proxyDir = null;
15$cache = null;
16$useSimpleAnnotationReader = false;
17
18// Connexion à la base de données
19$dbParams = [
20    'driver'   => 'pdo_mysql',
21    'host'     => 'localhost',
22    'charset'  => 'utf8',
23    'user'     => 'root',
24    'password' => '123456',
25    'dbname'   => 'todo',
26];
27
28$config = Setup::createAnnotationMetadataConfiguration(
29    $entitiesPath,
30    $isDevMode,
31    $proxyDir,
32    $cache,
33    $useSimpleAnnotationReader
34);
35$entityManager = EntityManager::create($dbParams, $config);
36
37return $entityManager;

Create file cli-config.php

1<?php
2# cli-config.php
3
4$entityManager = require_once join(DIRECTORY_SEPARATOR, [__DIR__, 'bootstrap.php']);
5
6use Doctrine\ORM\Tools\Console\ConsoleRunner;
7
8return ConsoleRunner::createHelperSet($entityManager);

Create a Database called like in the bootstrap.php file

 1// Task.php
 2/**
 3     * @ORM\OneToMany(targetEntity=Task::class, mappedBy="user")
 4     */
 5    protected $task;
 6
 7// User.php
 8/**
 9     * @ORM\ManyToOne(targetEntity=User::class, inversedBy="task")
10     */
11    protected $user;
 1use Doctrine\Common\Collections\ArrayCollection;
 2
 3/**
 4     * @ORM\ManyToMany(targetEntity=Poll::class)
 5     */
 6    protected $polls;
 7
 8    public function __construct()
 9    {
10        $this->polls = new ArrayCollection();
11        $this->participations = new ArrayCollection();
12    }