ICT-150 Doc

Post #194 written by Khodok in Code

Content

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

PHP
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
# src/Entity/User.php

namespace Tuto\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(
 *       name="users",
 *       indexes={
 *           @ORM\Index(name="search_firstname_lastname", columns={"firstname", "lastname"}),
 *           @ORM\Index(name="search_role", columns={"role"})
 *       }
 *   )
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue)
     * @ORM\Column(type="integer")
     */
    protected $id;
    /**
     * @ORM\Column(type="string")
     */
    protected $firstname;

    /**
     * @ORM\Column(type="string")
     */
    protected $lastname;

    /**
     * @ORM\Column(type="string")
     */
    protected $role;

    /**
     * @ORM\ManyToMany(targetEntity=Poll::class)
     */
    protected $polls;

    /**
     * @ORM\OneToMany(targetEntity=Participation::class, mappedBy="user")
     */
    protected $participations;

    /**
     * @ORM\OneToOne(targetEntity=Address::class, cascade={"persist", "remove"}, inversedBy="user")
     */
    protected $address;

    public function __construct()
    {
        $this->polls = new ArrayCollection();
        $this->participations = new ArrayCollection();
    }

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

    public function setId($id)
    {
        $this->id = $id;
    }

    public function getFirstname()
    {
        return $this->firstname;
    }

    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;
    }

    public function getLastname()
    {
        return $this->lastname;
    }

    public function setLastname($lastname)
    {
        $this->lastname = $lastname;
    }

    public function getRole()
    {
        return $this->role;
    }

    public function setRole($role)
    {
        $this->role = $role;
    }

    /**
     * @return mixed
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * @param mixed $address
     */
    public function setAddress($address): void
    {
        $this->address = $address;
    }

    public function __toString()
    {
        $format = "User (id: %s, firstname: %s, lastname: %s, role: %s, address: %s)\n";
        return sprintf($format, $this->id, $this->firstname, $this->lastname, $this->role, $this->address);
    }

    /**
     * @return ArrayCollection
     */
    public function getPolls(): ArrayCollection
    {
        return $this->polls;
    }

    /**
     * @param ArrayCollection $polls
     */
    public function setPolls(ArrayCollection $polls): void
    {
        $this->polls = $polls;
    }

    /**
     * @return ArrayCollection
     */
    public function getParticipations()
    {
        return $this->participations;
    }

    /**
     * @param ArrayCollection $participations
     */
    public function setParticipations(ArrayCollection $participations): void
    {
        $this->participations = $participations;
    }
}
PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
# create-user.php

$entityManager = require_once join(DIRECTORY_SEPARATOR, [__DIR__, 'bootstrap.php']);

use Tuto\Entity\User;

$admin = new User();
$admin->setFirstname("First ");
$admin->setLastname("LAST ");
$admin->setRole("admin");
$entityManager->persist($admin);
echo $admin . "</br>";

foreach (range(1, 10) as $index) {
    $user = new User();
    $user->setFirstname("First ".$index);
    $user->setLastname("LAST ".$index);
    $user->setRole("user");
    $entityManager->persist($user);
    echo $user . "</br>";
}

$entityManager->flush();
PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
# get-user.php

$entityManager = require_once join(DIRECTORY_SEPARATOR, [__DIR__, 'bootstrap.php']);

use Tuto\Entity\User;

$userRepo = $entityManager->getRepository(User::class);

$user = $userRepo->find(1);
echo "User by primary key: </br>";
echo $user . "</br>";

$allUsers = $userRepo->findAll();
echo "</br>All users: </br>";
foreach ($allUsers as $user) {
    echo $user . "</br>";
}

$usersByRole = $userRepo->findBy(["role" => "admin"]);
echo "</br>Users by role: </br>";
foreach ($usersByRole as $user) {
    echo $user . "</br>";
}

$usersByRoleAndFirstname = $userRepo->findBy(["role" => "user", "firstname" => "First 2"]);
echo "</br>Users by role and firstname: </br>";
foreach ($usersByRoleAndFirstname as $user) {
    echo $user . "</br>";
}

$limit = 4;
$offset = 2;
$orderBy = ["firstname" => "DESC"];
$usersByRoleWithFilters = $userRepo->findBy(["role" => "user"], $orderBy, $limit, $offset);
echo "</br>Users by role with filters: </br>";
foreach ($usersByRoleWithFilters as $user) {
    echo $user . "</br>";
}

//$usersByRole = $userRepo->findBy(["role" => "admin"]);
$usersByRole = $userRepo->findByRole("admin");
echo "</br>Users by role: </br>";
foreach ($usersByRole as $user) {
    echo $user . "</br>";
}
PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?php
# update-user.php

$entityManager = require_once join(DIRECTORY_SEPARATOR, [__DIR__, 'bootstrap.php']);

use Tuto\Entity\User;

$identifiant = 11;

$userRepo = $entityManager->getRepository(User::class);

// Récupération de l'utilisateur (donc automatiquement géré par Doctrine)
$user = $userRepo->find($identifiant);

$user->setFirstname("First Real Modification");
$user->setLastname("Last Real Modification");

$entityManager->flush();
PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
# delete-user.php

$entityManager = require_once join(DIRECTORY_SEPARATOR, [__DIR__, 'bootstrap.php']);

use Tuto\Entity\User;

$identifiant = 2;

$userRepo = $entityManager->getRepository(User::class);

// Récupération de l'utilisateur (donc automatiquement géré par Doctrine)
$user = $userRepo->find($identifiant);

$entityManager->remove($user);
$entityManager->flush($user);

// Récupération pour vérifier la suppression effective de l'utilisateur
$user = $userRepo->find($identifiant);

var_dump($user); // doit renvoyer NULL

composer require doctrine/orm:^2.8.*

Create a file bootstrap.php

PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
# bootstrap.php

require_once join(DIRECTORY_SEPARATOR, [__DIR__, 'vendor', 'autoload.php']);

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$entitiesPath = [
    join(DIRECTORY_SEPARATOR, [__DIR__, "src", "Entity"])
];

$isDevMode = true;
$proxyDir = null;
$cache = null;
$useSimpleAnnotationReader = false;

// Connexion à la base de données
$dbParams = [
    'driver'   => 'pdo_mysql',
    'host'     => 'localhost',
    'charset'  => 'utf8',
    'user'     => 'root',
    'password' => '123456',
    'dbname'   => 'todo',
];

$config = Setup::createAnnotationMetadataConfiguration(
    $entitiesPath,
    $isDevMode,
    $proxyDir,
    $cache,
    $useSimpleAnnotationReader
);
$entityManager = EntityManager::create($dbParams, $config);

return $entityManager;

Create file cli-config.php

PHP
1
2
3
4
5
6
7
8
<?php
# cli-config.php

$entityManager = require_once join(DIRECTORY_SEPARATOR, [__DIR__, 'bootstrap.php']);

use Doctrine\ORM\Tools\Console\ConsoleRunner;

return ConsoleRunner::createHelperSet($entityManager);

Create a Database called like in the bootstrap.php file

PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Task.php
/**
     * @ORM\OneToMany(targetEntity=Task::class, mappedBy="user")
     */
    protected $task;

// User.php
/**
     * @ORM\ManyToOne(targetEntity=User::class, inversedBy="task")
     */
    protected $user;
PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use Doctrine\Common\Collections\ArrayCollection;

/**
     * @ORM\ManyToMany(targetEntity=Poll::class)
     */
    protected $polls;

    public function __construct()
    {
        $this->polls = new ArrayCollection();
        $this->participations = new ArrayCollection();
    }
Comments

Please Log in to leave a comment.

No comments yet.