Eric Tu
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Getting Started with Doctrine This guide covers getting started with the Doctrine ORM. After working through the guide you should know: How to install and configure Doctrine by connecting it to a database Mapping PHP objects to database tables Generating a database schema from PHP objects Using the EntityManager to insert, update, delete and find objects in the datab # Guide Assumptions This guide is designed for beginners that haven’t worked with Doctrine ORM before. There are some prerequesites for the tutorial that have to be installed: PHP (latest stable version) Composer Package Manager (Install Composer) The code of this tutorial is available on Github. # What is Doctrine? Doctrine 2 is an object-relational mapper (ORM) for PHP 5.4+ that provides transparent persistence for PHP objects. It uses the Data Mapper pattern at the heart, aiming for a complete separation of your domain/business logic from the persistence in a relational database management system. The benefit of Doctrine for the programmer is the ability to focus on the object-oriented business logic and worry about persistence only as a secondary problem. This doesn’t mean persistence is downplayed by Doctrine 2, however it is our belief that there are considerable benefits for object-oriented programming if persistence and entities are kept separated. # What are Entities? Entities are PHP Objects that can be identified over many requests by a unique identifier or primary key. These classes don’t need to extend any abstract base class or interface. An entity class must not be final or contain final methods. Additionally it must not implement clone nor wakeup, unless it does so safely. An entity contains persistable properties. A persistable property is an instance variable of the entity that is saved into and retrieved from the database by Doctrine’s data mapping capabilities. # Project Setup ```bash= mkdir LearnDoctrine cd LearnDoctrine vim composer.json ``` ``` { "require": { "doctrine/orm": "2.4.*", "symfony/yaml": "2.*" }, "autoload": { "psr-0": {"": "src/"} } } ``` ``` composer install ``` Add the following directories: ``` doctrine2-tutorial |-- config | |-- xml | `-- yaml `-- src ``` ``` { "require": { "doctrine/orm": "2.4.*", "symfony/yaml": "2.*" }, "autoload": { "psr-0": {"": "src/"} } } ``` # Obtaining the EntityManager Doctrine’s public interface is the EntityManager, it provides the access point to the complete lifecycle management of your entities and transforms entities from and back to persistence. You have to configure and create it to use your entities with Doctrine 2. I will show the configuration steps and then discuss them step by step: ```php= <?php // bootstrap.php use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; require_once "vendor/autoload.php"; // Create a simple "default" Doctrine ORM configuration for Annotations $isDevMode = true; $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode); // or if you prefer yaml or XML //$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode); //$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode); // database configuration parameters $conn = array( 'driver' => 'pdo_sqlite', 'path' => __DIR__ . '/db.sqlite', ); // obtaining the entity manager $entityManager = EntityManager::create($conn, $config); ``` The first require statement sets up the autoloading capabilities of Doctrine using the Composer autoload. The second block consists of the instantiation of the ORM Configuration object using the Setup helper. It assumes a bunch of defaults that you don’t have to bother about for now. You can read up on the configuration details in the reference chapter on configuration. The third block shows the configuration options required to connect to a database, in my case a file-based sqlite database. All the configuration options for all the shipped drivers are given in the DBAL Configuration section of the manual. The last block shows how the EntityManager is obtained from a factory method. # Generating the Database Schema Now that we have defined the Metadata mappings and bootstrapped the EntityManager we want to generate the relational database schema from it. Doctrine has a Command-Line Interface that allows you to access the SchemaTool, a component that generates the required tables to work with the metadata. For the command-line tool to work a cli-config.php file has to be present in the project root directory, where you will execute the doctrine command. Its a fairly simple file: ```php= <?php // cli-config.php require_once "bootstrap.php"; return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager); ``` You can then change into your project directory and call the Doctrine command-line tool: ```bash= $ cd project/ $ vendor/bin/doctrine orm:schema-tool:create ``` At this point no entity metadata exists in src so you will see a message like “No Metadata Classes to process.” Don’t worry, we’ll create a Product entity and corresponding metadata in the next section. You can easily recreate the database: ```bash= $ vendor/bin/doctrine orm:schema-tool:drop --force $ vendor/bin/doctrine orm:schema-tool:create ``` Or use the update functionality: ```bash= $ vendor/bin/doctrine orm:schema-tool:update --force ``` # php bin/console doctrine:schema:update --force # php bin/console doctrine:schema:update --force # php bin/console doctrine:schema:update --force # php bin/console doctrine:schema:update --force The updating of databases uses a Diff Algorithm for a given Database Schema, a cornerstone of the Doctrine\DBAL package, which can even be used without the Doctrine ORM package. # Starting with the Product We start with the simplest entity, the Product. Create a src/Product.php file to contain the Product entity definition: ```php= <?php // src/Product.php class Product { /** * @var int */ protected $id; /** * @var string */ protected $name; public function getId() { return $this->id; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } } ``` Note that all fields are set to protected (not public) with a mutator (getter and setter) defined for every field except $id. The use of mutators allows Doctrine to hook into calls which manipulate the entities in ways that it could not if you just directly set the values with entity#field = foo; The id field has no setter since, generally speaking, your code should not set this value since it represents a database id value. (Note that Doctrine itself can still set the value using the Reflection API instead of a defined setter function) The next step for persistence with Doctrine is to describe the structure of the Product entity to Doctrine using a metadata language. The metadata language describes how entities, their properties and references should be persisted and what constraints should be applied to them. Metadata for entities are configured using a XML, YAML or Docblock Annotations. This Getting Started Guide will show the mappings for all Mapping Drivers. References in the text will be made to the XML mapping. ```php= <?php // src/Product.php /** * @Entity @Table(name="products") **/ class Product { /** @Id @Column(type="integer") @GeneratedValue **/ protected $id; /** @Column(type="string") **/ protected $name; // .. (other code) } ``` ```php= <?php // src/Product.php /** * @Entity @Table(name="products") **/ class Product { /** @Id @Column(type="integer") @GeneratedValue **/ protected $id; /** @Column(type="string") **/ protected $name; // .. (other code) } ``` XML YAML The top-level entity definition tag specifies information about the class and table-name. The primitive type Product#name is defined as a field attribute. The id property is defined with the id tag, this has a generator tag nested inside which defines that the primary key generation mechanism automatically uses the database platforms native id generation strategy (for example AUTO INCREMENT in the case of MySql or Sequences in the case of PostgreSql and Oracle). Now that we have defined our first entity, let’s update the database: ```bash= $ vendor/bin/doctrine orm:schema-tool:update --force --dump-sql ``` Specifying both flags --force and --dump-sql prints and executes the DDL statements. Now create a new script that will insert products into the database: ```php= <?php // create_product.php require_once "bootstrap.php"; $newProductName = $argv[1]; $product = new Product(); $product->setName($newProductName); $entityManager->persist($product); $entityManager->flush(); echo "Created Product with ID " . $product->getId() . "\n"; ``` Call this script from the command-line to see how new products are created: ```bash= $ php create_product.php ORM $ php create_product.php DBAL ``` What is happening here? Using the Product is pretty standard OOP. The interesting bits are the use of the EntityManager service. To notify the EntityManager that a new entity should be inserted into the database you have to call persist(). To initiate a transaction to actually perform the insertion, You have to explicitly call flush() on the EntityManager. This distinction between persist and flush is allows to aggregate all writes (INSERT, UPDATE, DELETE) into one single transaction, which is executed when flush is called. Using this approach the write-performance is significantly better than in a scenario where updates are done for each entity in isolation. Doctrine follows the UnitOfWork pattern which additionally detects all entities that were fetched and have changed during the request. You don’t have to keep track of entities yourself, when Doctrine already knows about them. --- ```bash= [eric_tu@localhost LearnDoctrine]$ vim src/Product.php [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force --dump-sql CREATE TABLE products (id INTEGER NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)); Updating database schema... Database schema updated successfully! "1" queries were executed [eric_tu@localhost LearnDoctrine]$ ls bootstrap.php cli-config.php composer.json composer.lock config db.sqlite src vendor [eric_tu@localhost LearnDoctrine]$ vim creat_product.php [eric_tu@localhost LearnDoctrine]$ php creat_product.php ORM PHP Fatal error: Call to undefined method Product::Id() in /usr/share/nginx/html/LearnDoctrine/creat_product.php on line 15 [eric_tu@localhost LearnDoctrine]$ vim creat_product.php [eric_tu@localhost LearnDoctrine]$ vim creat_product.php [eric_tu@localhost LearnDoctrine]$ php creat_product.php ORM Created Product wiht ID 2 [eric_tu@localhost LearnDoctrine]$ php creat_product.php DBAL Created Product wiht ID 3 ``` --- As a next step we want to fetch a list of all the Products. Let’s create a new script for this: ```php= <?php // list_products.php require_once "bootstrap.php"; $productRepository = $entityManager->getRepository('Product'); $products = $productRepository->findAll(); foreach ($products as $product) { echo sprintf("-%s\n", $product->getName()); } ``` The EntityManager#getRepository() method can create a finder object (called a repository) for every entity. It is provided by Doctrine and contains some finder methods such as findAll(). Let’s continue with displaying the name of a product based on its ID: ```php= <?php // show_product.php <id> require_once "bootstrap.php"; $id = $argv[1]; $product = $entityManager->find('Product', $id); if ($product === null) { echo "No product found.\n"; exit(1); } echo sprintf("-%s\n", $product->getName()); ``` Updating a product name demonstrates the functionality UnitOfWork of pattern discussed before. We only need to find a product entity and all changes to its properties are written to the database: ```php= <?php // update_product.php <id> <new-name> require_once "bootstrap.php"; $id = $argv[1]; $newName = $argv[2]; $product = $entityManager->find('Product', $id); if ($product === null) { echo "Product $id does not exist.\n"; exit(1); } $product->setName($newName); $entityManager->flush(); ``` After calling this script on one of the existing products, you can verify the product name changed by calling the show_product.php script. ```php= [eric_tu@localhost LearnDoctrine]$ php show_product.php PHP Notice: Undefined offset: 1 in /usr/share/nginx/html/LearnDoctrine/show_product.php on line 7 PHP Fatal error: Uncaught exception 'Doctrine\ORM\ORMException' with message 'The identifier id is missing for a query of Product' in /usr/share/nginx/html/LearnDoctrine/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php:259 Stack trace: #0 /usr/share/nginx/html/LearnDoctrine/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(378): Doctrine\ORM\ORMException::missingIdentifierField('Product', 'id') #1 /usr/share/nginx/html/LearnDoctrine/show_product.php(8): Doctrine\ORM\EntityManager->find('Product', NULL) #2 {main} thrown in /usr/share/nginx/html/LearnDoctrine/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php on line 259 ``` # Adding Bug and User Entities We continue with the bug tracker domain, by creating the missing classes Bug and User and putting them into src/Bug.php and src/User.php respectively. ```php= <?php // src/Bug.php /** * @Entity(repositoryClass="BugRepository") @Table(name="bugs") */ class Bug { /** * @Id @Column(type="integer") @GeneratedValue * @var int */ protected $id; /** * @Column(type="string") * @var string */ protected $description; /** * @Column(type="datetime") * @var DateTime */ protected $created; /** * @Column(type="string") * @var string */ protected $status; public function getId() { return $this->id; } public function getDescription() { return $this->description; } public function setDescription($description) { $this->description = $description; } public function setCreated(DateTime $created) { $this->created = $created; } public function getCreated() { return $this->created; } public function setStatus($status) { $this->status = $status; } public function getStatus() { return $this->status; } } ``` ```php= <?php // src/User.php /** * @Entity @Table(name="users") */ class User { /** * @Id @GeneratedValue @Column(type="integer") * @var int */ protected $id; /** * @Column(type="string") * @var string */ protected $name; public function getId() { return $this->id; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } } ``` # ? All of the properties discussed so far are simple string and integer values, for example the id fields of the entities, their names, description, status and change dates. Next we will model the dynamic relationships between the entities by defining the references between entities. References between objects are foreign keys in the database. You never have to (and never should) work with the foreign keys directly, only with the objects that represent the foreign key through their own identity. For every foreign key you either have a Doctrine ManyToOne or OneToOne association. On the inverse sides of these foreign keys you can have OneToMany associations. Obviously you can have ManyToMany associations that connect two tables with each other through a join table with two foreign keys. --- Now that you know the basics about references in Doctrine, we can extend the domain model to match the requirements: ```php= <?php // src/Bug.php use Doctrine\Common\Collections\ArrayCollection; class Bug { // ... (previous code) protected $products; public function __construct() { $this->products = new ArrayCollection(); } } ``` ```php= <?php // src/User.php use Doctrine\Common\Collections\ArrayCollection; class User { // ... (previous code) protected $reportedBugs; protected $assignedBugs; public function __construct() { $this->reportedBugs = new ArrayCollection(); $this->assignedBugs = new ArrayCollection(); } } ``` You use Doctrine’s ArrayCollections in your Doctrine models, rather than plain PHP arrays, so that Doctrine can watch what happens with them and act appropriately. Note that if you dump your entities, you’ll see a “PersistentCollection” in place of your ArrayCollection, which is just an internal Doctrine class with the same interface. ```bash= [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force [Doctrine\Common\Annotations\AnnotationException] [Syntax Error] Expected Doctrine\Common\Annotations\DocLexer::T_IDENTIFIER or Doctrine\Common\Annotations\DocLexer::T_TRUE or D octrine\Common\Annotations\DocLexer::T_FALSE or Doctrine\Common\Annotations\DocLexer::T_NULL, got '@' at position 52 in propert y Bug::$id. ``` id 前面的innoation 一個一個參數砍掉檢查 發現砍掉最後一個var int 之後就可以了, 把他加回去之後在試一次,又可以了!? 真神奇... ```bash= [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force Updating database schema... Database schema updated successfully! "2" queries were executed [eric_tu@localhost LearnDoctrine]$ vim s show_product.php src/ [eric_tu@localhost LearnDoctrine]$ vim src/Bug.php [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force Nothing to update - your database is already in sync with the current entity metadata. [eric_tu@localhost LearnDoctrine]$ vim src/Bug.php ``` ***Lazy load proxies always contain an instance of Doctrine’s EntityManager and all its dependencies. Therefore a var_dump() will possibly dump a very large recursive structure which is impossible to render and read. You have to use Doctrine\Common\Util\Debug::dump() to restrict the dumping to a human readable level. Additionally you should be aware that dumping the EntityManager to a Browser may take several minutes, and the Debug::dump() method just ignores any occurrences of it in Proxy instances.*** # est practices in handling database relations and Object-Relational Mapping Because we only work with collections for the references we must be careful to implement a bidirectional reference in the domain model. The concept of owning or inverse side of a relation is central to this notion and should always be kept in mind. The following assumptions are made about relations and have to be followed to be able to work with Doctrine 2. These assumptions are not unique to Doctrine 2 but are best practices in handling database relations and Object-Relational Mapping. - Changes to Collections are saved or updated, when the entity on the owning side of the collection is saved or updated. - Saving an Entity at the inverse side of a relation never triggers a persist operation to changes to the collection. - In a one-to-one relation the entity holding the foreign key of the related entity on its own database table is always the owning side of the relation. - In a many-to-many relation, both sides can be the owning side of the relation. However in a bi-directional many-to-many relation only one is allowed to be. - In a many-to-one relation the Many-side is the owning side by default, because it holds the foreign key. - The OneToMany side of a relation is inverse by default, since the foreign key is saved on the Many side. A OneToMany relation can only be the owning side, if its implemented using a ManyToMany relation with join table and restricting the one side to allow only UNIQUE values per database constraint. ***Consistency of bi-directional references on the inverse side of a relation have to be managed in userland application code. Doctrine cannot magically update your collections to be consistent.*** In the case of Users and Bugs we have references back and forth to the assigned and reported bugs from a user, making this relation bi-directional. We have to change the code to ensure consistency of the bi-directional reference: # 修改collection使符合雙向驅動 ```php= <?php // src/Bug.php class Bug { // ... (previous code) protected $engineer; protected $reporter; public function setEngineer($engineer) { $engineer->assignedToBug($this); $this->engineer = $engineer; } public function setReporter($reporter) { $reporter->addReportedBug($this); $this->reporter = $reporter; } public function getEngineer() { return $this->engineer; } public function getReporter() { return $this->reporter; } } ``` ```php= <?php // src/User.php class User { // ... (previous code) protected $reportedBugs = null; protected $assignedBugs = null; public function addReportedBug($bug) { $this->reportedBugs[] = $bug; } public function assignedToBug($bug) { $this->assignedBugs[] = $bug; } } ``` I chose to name the inverse methods in past-tense, which should indicate that the actual assigning has already taken place and the methods are only used for ensuring consistency of the references. This approach is my personal preference, you can choose whatever method to make this work. You can see from User#addReportedBug() and User#assignedToBug() that using this method in userland alone would not add the Bug to the collection of the owning side in Bug#reporter or Bug#engineer. Using these methods and calling Doctrine for persistence would not update the collections representation in the database. Only using Bug#setEngineer() or Bug#setReporter() correctly saves the relation information. 將錯誤回報隔開 The Bug#reporter and Bug#engineer properties are Many-To-One relations, which point to a User. In a normalized relational model the foreign key is saved on the Bug’s table, hence in our object-relation model the Bug is at the owning side of the relation. You should always make sure that the use-cases of your domain model should drive which side is an inverse or owning one in your Doctrine mapping. In our example, whenever a new bug is saved or an engineer is assigned to the bug, we don’t want to update the User to persist the reference, but the Bug. This is the case with the Bug being at the owning side of the relation. Bugs reference Products by an uni-directional ManyToMany relation in the database that points from Bugs to Products. ```php= <?php // src/Bug.php class Bug { // ... (previous code) protected $products = null; public function assignToProduct($product) { $this->products[] = $product; } public function getProducts() { return $this->products; } } ``` --- # Lets add metadata mappings for the User and Bug We are now finished with the domain model given the requirements. Lets add metadata mappings for the User and Bug as we did for the Product before: ```php= <?php // src/Bug.php /** * @Entity @Table(name="bugs") **/ class Bug { /** * @Id @Column(type="integer") @GeneratedValue **/ protected $id; /** * @Column(type="string") **/ protected $description; /** * @Column(type="datetime") **/ protected $created; /** * @Column(type="string") **/ protected $status; /** * @ManyToOne(targetEntity="User", inversedBy="assignedBugs") **/ protected $engineer; /** * @ManyToOne(targetEntity="User", inversedBy="reportedBugs") **/ protected $reporter; /** * @ManyToMany(targetEntity="Product") **/ protected $products; // ... (other code) } ``` ```php= <?php // src/User.php /** * @Entity @Table(name="users") **/ class User { /** * @Id @GeneratedValue @Column(type="integer") * @var int **/ protected $id; /** * @Column(type="string") * @var string **/ protected $name; /** * @OneToMany(targetEntity="Bug", mappedBy="reporter") * @var Bug[] **/ protected $reportedBugs = null; /** * @OneToMany(targetEntity="Bug", mappedBy="engineer") * @var Bug[] **/ protected $assignedBugs = null; // .. (other code) } ``` ```bash= [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force PHP Fatal error: Cannot redeclare Bug::$products in /usr/share/nginx/html/LearnDoctrine/src/Bug.php on line 96 ``` ```bash= [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force PHP Fatal error: Cannot redeclare User::$reportedBugs in /usr/share/nginx/html/LearnDoctrine/src/User.php on line 56 ``` ```bash= [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force PHP Fatal error: Cannot redeclare Bug::$engineer in /usr/share/nginx/html/LearnDoctrine/src/Bug.php on line 106 ``` ```bash= [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force PHP Fatal error: Cannot redeclare User::$assignedBugs in /usr/share/nginx/html/LearnDoctrine/src/User.php on line 58 ``` ```bash= [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force PHP Fatal error: Cannot redeclare Bug::$reporter in /usr/share/nginx/html/LearnDoctrine/src/Bug.php on line 109 ``` ```bash= [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force PHP Fatal error: Cannot redeclare User::$reportedBugs in /usr/share/nginx/html/LearnDoctrine/src/User.php on line 68 ``` ```bash= [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force PHP Fatal error: Cannot redeclare Bug::$products in /usr/share/nginx/html/LearnDoctrine/src/Bug.php on line 135 ``` ```bash= [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force PHP Fatal error: Cannot redeclare User::$assignedBugs in /usr/share/nginx/html/LearnDoctrine/src/User.php on line 70 ``` --- ```bash= [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force PHP Parse error: syntax error, unexpected '@', expecting variable (T_VARIABLE) in /usr/share/nginx/html/LearnDoctrine/src/Bug.php on line 138 ``` ### Database schema updated successfully! ```bash= [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force Updating database schema... Database schema updated successfully! "10" queries were executed ``` --- # Implementing more Requirements ### create user entities ```php= <?php // create_user.php require_once "bootstrap.php"; $newUsername = $argv[1]; $user = new User(); $user->setName($newUsername); $entityManager->persist($user); $entityManager->flush(); echo "Created User with ID " . $user->getId() . "\n"; ``` ```bash= [eric_tu@localhost LearnDoctrine]$ php creat_user.php beberlei Created User with ID 1 ``` # We now have the data to create a bug and the code for this scenario may look like this: ```php= <?php // create_bug.php require_once "bootstrap.php"; $theReporterId = $argv[1]; $theDefaultEngineerId = $argv[2]; $productIds = explode(",", $argv[3]); $reporter = $entityManager->find("User", $theReporterId); $engineer = $entityManager->find("User", $theDefaultEngineerId); if (!$reporter || !$engineer) { echo "No reporter and/or engineer found for the input.\n"; exit(1); } $bug = new Bug(); $bug->setDescription("Something does not work!"); $bug->setCreated(new DateTime("now")); $bug->setStatus("OPEN"); foreach ($productIds as $productId) { $product = $entityManager->find("Product", $productId); $bug->assignToProduct($product); } $bug->setReporter($reporter); $bug->setEngineer($engineer); $entityManager->persist($bug); $entityManager->flush(); echo "Your new Bug Id: ".$bug->getId()."\n"; ``` ```bash= [eric_tu@localhost LearnDoctrine]$ vendor/bin/doctrine orm:schema-tool:update --force Updating database schema... Database schema updated successfully! "18" queries were executed [eric_tu@localhost LearnDoctrine]$ php create_bug.php 1 1 1 PHP Notice: Undefined variable: theReporterId in /usr/share/nginx/html/LearnDoctrine/create_bug.php on line 10 PHP Fatal error: Uncaught exception 'Doctrine\ORM\ORMException' with message 'The identifier id is missing for a query of User' in /usr/share/nginx/html/LearnDoctrine/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php:259 Stack trace: #0 /usr/share/nginx/html/LearnDoctrine/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(378): Doctrine\ORM\ORMException::missingIdentifierField('User', 'id') #1 /usr/share/nginx/html/LearnDoctrine/create_bug.php(10): Doctrine\ORM\EntityManager->find('User', NULL) #2 {main} thrown in /usr/share/nginx/html/LearnDoctrine/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php on line 259 ``` ```bash= [eric_tu@localhost LearnDoctrine]$ php create_bug.php 1 1 1 PHP Fatal error: Call to undefined method Doctrine\ORM\EntityManager::ifnd() in /usr/share/nginx/html/LearnDoctrine/create_bug.php on line 24 [eric_tu@localhost LearnDoc ``` ```bash= [eric_tu@localhost LearnDoctrine]$ php create_bug.php 1 1 1 PHP Fatal error: Call to undefined method User::addReporter() in /usr/share/nginx/html/LearnDoctrine/src/Bug.php on line 119 ``` 119 $reporter->addReportedBug($this); ### success ```bash= [eric_tu@localhost LearnDoctrine]$ php create_bug.php 1 1 1 Your new Bug Id: 1 ``` This is the first contact with the read API of the EntityManager, showing that a call to EntityManager#find($name, $id) returns a single instance of an entity queried by primary key. Besides this we see the persist + flush pattern again to save the Bug into the database. See how simple relating Bug, Reporter, Engineer and Products is done by using the discussed methods in the “A first prototype” section. The UnitOfWork will detect this relationship when flush is called and relate them in the database appropriately. # Queries for Application Use-Cases # List of Bugs Using the previous examples we can fill up the database quite a bit, however we now need to discuss how to query the underlying mapper for the required view representations. When opening the application, bugs can be paginated through a list-view, which is the first read-only use-case:

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully