Zend Basic Tutorial
Zend Forms
Zend Database
Zend Advanced
I’ll now give you a complete CRUD (Create, Read, Update, Delete) implementation using Doctrine ORM in Zend Framework, along with additional queries and clear explanations at each step.
User
EntityLet’s assume we have a User
entity with the following fields:
id
: integer (primary key)name
: stringInject EntityManager link
User
Entity (module/Application/src/Entity/User.php)namespace Application\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="user") */ class User { /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=255) */ private $name; // Getters and setters... public function getId(): ?int { return $this->id; } public function getName(): ?string { return $this->name; } public function setName(string $name): self { $this->name = $name; return $this; } }
public function createAction() { $user = new User(); $user->setName('John'); $user->setEmail('john@example.com'); $user->setIsActive(true); $this->entityManager->persist($user); $this->entityManager->flush(); return new JsonModel(['message' => 'User created']); }
public function indexAction() { $users = $this->entityManager->getRepository(User::class)->findAll(); return new JsonModel(['users' => $users]); }
public function updateAction() { $user = $this->entityManager->find(User::class, 1); if ($user) { $user->setName('Jane'); $this->entityManager->flush(); } return new JsonModel(['message' => 'User updated']); }
public function deleteAction() { $user = $this->entityManager->find(User::class, 1); if ($user) { $this->entityManager->remove($user); $this->entityManager->flush(); } return new JsonModel(['message' => 'User deleted']); }
$user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => 'john@example.com']);
$users = $this->entityManager->getRepository(User::class)->findBy(['is_active' => true]);
$query = $this->entityManager->createQuery('SELECT u FROM Application\Entity\User u WHERE u.is_active = 1'); $activeUsers = $query->getResult();