CodeSamplez.com

Programming, Web development, Cloud Technologies

  • Facebook
  • Google+
  • RSS
  • Twitter
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact
Home Database Basics On Doctrine CRUD Operations

Basics On Doctrine CRUD Operations

Rana Ahsan July 18, 2012 3 Comments


 Basics On Doctrine CRUD Operations    

Some developers don’t start using doctrine in their application, just because, they find it a little difficult to get started(I listened so from some of my own friends too). And unluckily, I also faced some similar issue as well. I tried to start it at very early time when I am new in LAMP(Linux, Apache,MySQL And PHP Environment) and found it a little difficult to understand from the official documentation for installation and getting started smoothly. I had to take help of several online tutorials as well. On an earlier post, I have already discussed about basic usage and integration of Doctrine ORM with Codeigniter framework. Today, in this tutorial, I am going to give some php code examples and show how we can perform the doctrine CRUD operations.

Important Note: I will be using same contact entity used on the earlier tutorial. So, if you haven’t read the previous tutorial, at least get an idea of the entity structure from there.

Select/Read Operation:

To select a single entity by its identifier: This is a common need and thus doctrine made the work easy for us with its integrated function. You can pass the entity name and the identifier value to the “find” function of doctrine entity manager and it will return you a single entity object. Code is as follows:

function get_single($id)
    {
        try
        {     
            $city = $this->em->find("PdContact",$id);
            return $city;
        }
        catch(Exception $err)
        {
            return NULL;
        }
    }

Another thing, that need to be remembered here is that, the identifier value must be the value of primary key column. Suppose, you have two columns in your database table named ‘id’ and ’email’ and you made the ’email’ column as primary key(may be used the ‘id’ for auto increment). In this case, doctrine will search for the entity via email column in database table with the given identifier value, not the id column.

To records in a specified range and with/without constraint: This often required in case, where data are shown on a data grid and have paging like system to show only certain number of data. You may have some conditions as well, like retrieve only active members,approved messages, join more than one table etc. The example code for such cases is as follows:

 /**
     * Return list of records according to given start index and length
     * @param Int $start the start index number for the result entity list
     * @param Int $length Determines how many records to fetch
     * @param Array $criteria specify where conditions
     * @param String $orderby specify columns, in which data should be ordered
     * @return type 
     */
    function get_by_range($start=0,$length=10,$criteria = NULL,$orderby=NULL)
    {
        try
        {
            return $this->em->getRepository("PdContact")->findBy($criteria, $orderby, $length, $start);
        }
        catch(Exception $err)
        {
            return NULL;
        }
    }

Retrieve total row count for an entity in database: I didn’t find any built-in function for this purpose in doctrine(please correct me if I am wrong ๐Ÿ™‚ ) and thus we have to create a query for this purpose, which can be as follows(You can use DQL as an alternative as well):

    /**
     * Return the number of records
     * @return integer 
     */
    function get_count()
    {
        try
        {
            $query = $this->em->createQueryBuilder()
                            ->select("count(a)")
                            ->from("PdContact", "a")
                            ->getQuery();
            return $query->getSingleScalarResult();
        }
        catch(Exception $err)
        {
            return 0;
        }
    }

Here, I will like to clarify for your better understanding that, there are some similar function named ‘getSingle’ and ‘getScalarResult’ also. As their name implies, ‘getSingleResult’ will retrieve a single record, which have a list of properties and ‘getScalar’ will retrieve a list of records with a single property for each. So, for a single value(as like count), we should use ‘getSingleScalerResult’ method.

Insert/Create operation:

I already shared code snippet for this operation on my previous tutorial on using doctrine with codeigniter. However, that was in codeigniter format(taking the post input), so I am sharing it here again in regular format. Also, I will be writing the function in a little different way, you will know the reason very soon. Here we go:

/**
     * Save contact message to database
     * @param array $contact_form
     * @return bool 
     */
    function save_message($data,$id=NULL)
    {    
        /**
         * @var PdContact $contact
         */
        if(empty($id)){
            $contact = new PdContact();
        }
        else{
            $contact = $this->get_single($id);
        }
        $contact->setName($data["name"]);
        $contact->setEmail($data["email"]);
        $contact->setSubject($data["subject"]);
        $contact->setMessage($data["message"]);
        
        try {
            //save to database
            $this->em->persist($contact);
            $this->em->flush();
            return TRUE;
        }
        catch(Exception $err){
            return FALSE;
        }
        return true;        
    }

Here I am assuming that, you already have validate the submitted data(in either post or get method) and passing them in a $data named associative array. For insert purpose, use this as follows:

$classname->save_message($data);

class name is where these codes you are integrating. It need to be remembered, second value is not supposed to pass in this case. Then, it will assume it as null and create new entity, which will result a new insertion in database.

Update Operation:

here, actually, we won’t have to write anything new ๐Ÿ˜€ . The previous function will do our work by nature. to use that function for update purpose, follow this example:

$classname->save_message($data,$id);

Also, for update purpose, the above described function definition requires that, the previous ‘get_single’ method is also in the same class. When the function is called with an id parameter, it will retrieve the corresponding record from database, update the fields with new data and save it to database.

Delete Operation:

To delete an entity is some how straight cut way. Specify the entity and call ‘remove’ method on entity manager. And flush the operation. Following is the code example for such need, which I have generalized to accept either a single parameter or an array of parameter to ease deletion of single/multiple entitie(s) in the same function:

     /**
     * Delete an Entity according to given (list of) id(s)
     * @param type $ids array/single
     */
    function delete_entities($ids){
        try
        {
            if(!is_array($ids))
            {
                $ids = array($ids);
            }
            foreach($ids as $id)
            {
                $entity = $this->em->getPartialReference("PdContact", $id);
                $this->em->remove($entity);
            }
            $this->em->flush();
            return TRUE;
        }
        catch(Exception $err)
        {
            return FALSE;
        }
    }

Note the method call of “getPartialReference” . You could use simply “$this->get_single($id)” instead. However, in that case, it would retrieve the record from database and then you would perform the delete operation. And now, with getting partial reference, it won’t query the database at all. Thus, saving some query cost and optimize the performance(it would be noticeably slow if try to delete a lot of data at once).

Hope this doctrine crud tutorial will be helpful for you. Very soon, I am planning to write few more tutorials on doctrine covering other features of it. So, be in touch. Ask me if you have any questions as well. Happy coding ๐Ÿ™‚ .

Related

Filed Under: Database, Development Tagged With: doctrine, mysql, php

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Comments

  1. webof says

    August 9, 2013 at 4:03 am

    God bless you dude. You saved my life. I looked from videos to official docs of the Doctrine, they all suck. But this saved me. Really great tutorial. Hope you can do same for PHPUnit too ๐Ÿ˜‰

    Reply
  2. warrioranish5 says

    December 5, 2016 at 5:43 am

    while performing select/read operation when i am trying to load retrieved data in view page following error occurs

    Fatal error: Cannot access private property PdContact::$name in C:\xampp\htdocs\doctrine\application\views\retrievedata.php on line 10

    how can i show them in view page.

    Reply

Trackbacks

  1. Custom Codeigniter Doctrine Model With Basic Functionality | codesamplez.com says:
    November 21, 2012 at 12:28 am

    […] On an earlier tutorial, I did showed how we can easily perform the CRUD operations using doctrine ORM. However, as a lot of developers use doctrine ORM as part of their favorite Codeigntier framework, […]

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Email Subscription

Never miss any programming tutorial again.

Popular Tutorials

  • How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
  • LinQ Query With Like Operator
  • Facebook C# API Tutorials
  • LinQ To SQL Database Update Operations In C#
  • Using Supervisord Web Interface And Plugin
  • Tutorial On Uploading File With CodeIgniter Framework / PHP
  • Utilizing Config File In C#.NET Application
  • Using GIT Plugin For Netbeans IDE

Recent Tutorials

  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read
  • Getting Started With Golang Unit Testing
  • Getting Started With Big Data Analytics Pipeline
  • NodeJS Tips And Tricks For Beginners
  • Apple Push Notification Backend In NodeJS
  • Web Based Universal Language Translator, Voice/Text Messaging App
  • How To Dockerize A Multi-Container App From Scratch

Recent Comments

  • S. Chalisque on PHP HTML5 Video Streaming Tutorial
  • Armorik on Generate HTTP Requests using c#
  • iswaps on PHP HTML5 Video Streaming Tutorial
  • TAKONDWA on PHP HTML5 Video Streaming Tutorial
  • rorenzo on PHP HTML5 Video Streaming Tutorial

Archives

Resources

  • CodeSamplez.com Demo

Tags

.net apache api audio aws c# cache cloud server codeigniter deployment doctrine facebook git github golang htaccess html5 http image java javascript linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty ssh tfs thread tips ubuntu unit-test utility web application wordpress wpf

Copyright © 2010 - 2022 · CodeSamplez.com ·

Copyright © 2022 ยท Streamline Pro Theme on Genesis Framework ยท WordPress ยท Log in