Doctrine

Netbeans – Goto matching brace/bracket

If using Netbeans for your development and you have a horrendous spagetti coded “if” statement, you can use a keyboard shortcut to go to the matching opening or closing bracket.

Simply click on the first (or last) bracket, and then press CTRL + [ and Netbeans will move the cursor to the matching bracket. Its not CTRL + ] for the reverse, its CTRL + [ for both.

I can confirm that this works in Netbeans 7.0.1 on Linux and Mac (seeing as its a Java app, it should be the same on Windows too)

 

Message: [Syntax Error] Expected PlainValue, got ”’ at position x in property {model}

Was getting the above error in my Doctrine project, turned out I had used a single quote in the ORM Annotations:

    /**
     * @ORM\Column(type='integer', name='row_number')
     * @var int
     */
    protected $rowNumber;

Your ORM Annotations must be double quotes ” to allow the read ahead to read ahead…

Should be like:

    /**
     * @ORM\Column(type="integer", name="row_number")
     * @var int
     */
    protected $rowNumber;

Setting up Class Table Inheritance with Doctrine 2.0

Have had some serious problems getting this working, but after a 4 hour head bashing session, we’ve cracked it.

CREATE TABLE inventory(
    inventory_id INT AUTO_INCREMENT,
    discriminator_column VARCHAR(20),
    category VARCHAR(50) NOT NULL,
    part_number VARCHAR(50) NOT NULL,
    PRIMARY KEY (inventory_id),
    KEY category_part_number (category, part_number),
    KEY discriminator_column (discriminator_column)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE inventory_room(
    inventory_id INT NOT NULL,
    rack_limit INT(6),
    room_type VARCHAR(25),
    KEY inventory_id (inventory_id),
    CONSTRAINT inventory_room_key FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id) 
        ON DELETE CASCADE 
        ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

You have to have a discriminator field so that Doctrine can connect the tables nicely when its joining them.

<?php
namespace Net\Model\Inventory;
use Doctrine\ORM\Mapping as ORM;

/**
 * Inventory entity
 *
 * @ORM\Entity(repositoryClass="Net\Repository\Inventory")
 * @ORM\Table(name="inventory")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discriminator_column", type="string")
 * @ORM\DiscriminatorMap({"inventory" = "AbstractItem", "inventory_room" = "Room"})
 *
 * @category Net
 * @package  Model
 */
abstract class AbstractItem
{
    /**
     * @ORM\Id @ORM\Column(name="inventory_id") @ORM\GeneratedValue
     * @var int
     */
    protected $inventory_id;

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

    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $part_number;
 }

As you can see in the above class, we have a ORM\GeneratedValue field. This tells Doctrine that this field is an increment field. As this is a one-to-one relationship we do not need one in the inventory_room table as it will be joined on the inventory_id field.

In the next class, this is the child class that extends the abstract. So Doctrine will read the annotations at the top and see that its a joined table using the id field (inventory_id)

<?php
namespace Net\Model\Inventory;
use Doctrine\ORM\Mapping as ORM;

/**
 * Inventory Room entity
 * @ORM\Entity
 * @ORM\Table(name="inventory_room")
 *
 * @category Net
 * @package  Model
 */
class Room extends AbstractItem
{
    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $room_type;

    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $rack_limit;
}