MY_Model base CRUD |
[eluser]victorche[/eluser]
@mark, thanks! But anyway sorry for my stupid questions ... There are some cases when these functions are really great. Anyway I am having trouble with a simple query about getting comments for a specific post. I mean ... I can get them really easy with your functions, like: Code: function get_comments($id) Code: $added = nice_time($row['added']); Code: $query = $this->db->select('id', 'author', 'comment', 'added')->from('comments')
[eluser]Mark Croxton[/eluser]
get() returns a resultset, so you could iterate through it using foreach() But to be honest I think of date formatting as display logic, so I'd use nice_time() in the view instead. The controller shouldn't need to worry about how something looks.
[eluser]dkenzik[/eluser]
Here's a quick question, and probably something I overlooked, so feel free to berate me as necessary... What's the proper way to utilize OR and AND (or a combination of each) in the ->where(array()) ? And related... can the ->where() just accept a literal statement, similar to ->select("foo.bar",false) ?
[eluser]victorche[/eluser]
[quote author="Mark Croxton" date="1281036573"]get() returns a resultset, so you could iterate through it using foreach() But to be honest I think of date formatting as display logic, so I'd use nice_time() in the view instead. The controller shouldn't need to worry about how something looks.[/quote] Thanks, @Mark! But I am using the template parser, so my date in the views looks just like {added} Can you please give me an example how would you do it in this case? The query I mean ...
[eluser]Mark Croxton[/eluser]
@dkenzik Except for the ones I've overloaded (where(), select() etc), all the other CI Active record methods are available to use, you just don't need to reference the $db object and they can be chained. Eg: Code: $this->where(array('age >=' => '30'))->where_not_in('username', $names); You can pass a hardcoded string to where(), optionally passing the value in the second parameter if you want CI to protect the table names and fields with backticks: Code: $this->where('age >= 30'); If you do this though MY_Model won't check that 'age' actually exists as a column in the current table, and MY_Model will not record that the table has been used in the query. If you pass an array you will get the integrity checking, and table name and field will be enclosed within backticks. This is the way I recommend you use where() whenever possible: Code: $this->where(array('age >=' => '30')); If you want to generate a WHERE IN(value1, value2) then the value passed can be an array: Code: $this->where(array('age' => array('30', '31', '32'))); You can also hardcode the table names if you wish; if you do this then as before no integrity check will be done on the column. You will want to do this if you want to use SQL functions like LOWER, DATE_FORMAT etc Code: $this->where(array('LOWER(LEFT(my_table.last_name, 1)) =' => 'd'));
[eluser]Mark Croxton[/eluser]
Code: $comments = $this->with('comments')->get(
[eluser]victorche[/eluser]
[quote author="Mark Croxton" date="1281111101"] Code: $comments = $this->with('comments')->get( Thanks, Mark but ... I don't just need it like this. I need to use a small function which will turn the date into "2 days ago". So your example is purfect, but not exactly what I need :/
[eluser]Mark Croxton[/eluser]
get() returns a resultset array, so you can do whatever you like with it: Code: $comments = $this->with('comments')->get(
[eluser]MT206[/eluser]
Great base model. I really like how you address joins where almost all other my_models don't. Hopefully you keep this up. I am going to use this in my first big project that I am working on. One thing I would like to run by you is whether or not this is the best way to handle this or not. I use a soft delete for most things where I just switch between 1 and 0 in a deleted field to 'delete' a record. I have created a slightly modified version of your update method where the $data variable is instead hard-coded to the 'deleted' field. Am I going about this correctly? Code: /** |
Welcome Guest, Not a member yet? Register Sign In |