zen of coding

read() vs find()

What exactly is the differednce between $this->User->read() and $this->User->find()?

Seemingly, they both retrieve the data (and possibly associated data) for the User model. Yet, there is a an important distinction: read() will also set the data for your model object, while find() will simply return the resultset array.

What’s the big deal?
None, really. However, if you are not planning to manipulate the object in any way, what exactly is the point of populating it with any data?
If your goal is simply to get some data and pass it to the view for display, you should stick with using find(). As a matter of fact read() will call find() internally anyways.

That beings said, read() can probably be useful if you need to manipulate your model’s data and then save it. Surely the same can be done with find(), but read() will provide a cleaner syntax, if that’s what you are after.

I’m not going to address any issues with performance, because if you are running into bottlenecks due to the performance of read() vs find(), I’m sure you’ve got more pressing issues at hand to deal with (like saving your DB or purchasing a better server).

  • So would you think that it makes more sense to do a read() or find() within a View? Additionally, do you know if it is possible to ‘contain’ before doing a read() ??

  • @Chris

    Neither should be done in the view.
    Data should be accessed from the model in the controller and then passed to the view.

    … Not sure about ‘contain’, but off top of my head I’m going to say that it’s best used with find().

    • Ishrar

      @teknoid …So, can read() have field conditions, like find()?

      I need read() to find a record of which I do not know the $id.

      If it’s impossible, can you please tell me how can I write the find() with conditions, that I can use like read(), i.e. in a way that it will return the record in an array, which is suitable for Model::save().

  • teknoid


    read() cannot accept conditions like find().
    That being said find() should return the array in the format already.

%d bloggers like this: