Welcome Guest, Not a member yet? Register   Sign In
CRUD by Developer13 Question
#1

[eluser]spmckee[/eluser]
Greets,

I am using the CRUD method by at Developer13 http://www.developer13.com/index.php/dev...101-models and have a question about how to use it in the controller.

I can get the get() functino to work gret, even with the filter settings. But I can only seem to this once and can't return multiple filtered get().

For instance:
Code:
$this->Mdl_works->seo_url = $seo_url;
      $this->Mdl_works->active = "1";

      $data['works_detail'] = $this->Mdl_works->get();
works great. But how do I also get
Code:
$this->Mdl_works->active = "1";
      
      $data['works_grid'] = $this->Mdl_works->get();
?

Here's the whole thing:
Code:
function detail($seo_url)
  {
      //$this->output->cache(30);
      $this->load->helper('typography');
          
      //Only get active + SEO
      $this->Mdl_works->seo_url = $seo_url;
      $this->Mdl_works->active = "1";

      $data['works_detail'] = $this->Mdl_works->get();

      $data['title'] = "The Work / ".$data['works_detail']->project_name;
      $data['main'] = 'the_work_detail_view';
      $data['page'] = 'the_work';
      
      //Only get active
      $this->Mdl_works->active = "1";
      
      $data['works_grid'] = $this->Mdl_works->get();
      $this->load->view('template_view', $data);
  }

Thanks!
#2

[eluser]Developer13[/eluser]
Can you post your model?
#3

[eluser]spmckee[/eluser]
Sure thing D13.

Code:
<?php

class Mdl_works extends My_Model {

    // We will give this model the following properties so they can be set dynamically from the controller
    var $work_id;
    var $seo_url;
    var $active;

    function Mdl_works() {

        // All models need to call the constructor of the parent Model class
        parent::My_Model();
    }

    function get() {

        // BEGIN FILTER CRITERIA CHECK
        // If any of the following properties are set before mdl_contacts->get() is called from the controller then we will include
        // a where statement for each of the properties that have been set.
        if ($this->work_id) {
            $this->db->where("work_id", $this->work_id);
        }

        if ($this->seo_url) {
            $this->db->where("seo_url", $this->seo_url);
        }
        
        if ($this->active) {
            $this->db->where("active", $this->active);
        }
        
        // END FILTER CRITERIA CHECK

        // We will display our results in order by last name and then first name.
        //$this->db->orderby("last_name, first_name");

        // This will execute the query and collect the results and other properties of the query into an object.
        $query = $this->db->get("works");

        // If you set mdl_contacts->contact_id from your controller, then there will only be one row to return.
        if ($this->work_id) {
            return ($query->row());
        }

        // If mdl_contacts->contact_id was not specified in the controller, then we will return the results as a result set.
        else {
            return ($query->result());
        }
    }

    function save() {

        // When we insert or update a record in CodeIgniter, we pass the results as an array:
        $db_array = array(
        "project_name" => $this->project_name,
        "seo_url" => $this->seo_url,
        "client" => $this->client,
        "project_title" => $this->project_title,
        "img_title" => $this->img_title,
        "img_main_background" => $this->img_main_background,
        "active" => $this->active,
        "desc_short" => $this->desc_short,
        "img_web_thumb" => $this->image_web_thumb,
        "img_web_full" => $this->image_web_full,
        "desc_challenge" => $this->desc_challenge,
        "desc_thug_factor" => $this->desc_thug_factor
        );

        // If mdl_contacts->contact_id was set in the controller, then we will update an existing record.
        if ($this->work_id) {
            $this->db->where("work_id", $this->contact_id);
            $this->db->update("works", $db_array);
        }

        // If mdl_contacts->contact_id was not set in the controller, then we will insert a new record.
        else {
            $this->db->insert("works", $db_array);
        }
    }

    function delete() {

        // As long as mdl_contacts->contact_id was set in the controller, we will delete the record.
        if ($this->work_id) {
            $this->db->where("work_id", $this->work_id);
            $this->db->delete("works");
        }
    }
}
?>
#4

[eluser]Developer13[/eluser]
Oh ok, I can be a little slow at times Smile

You *could* use some type of function in the model to unset the properties.

For example:

function reset() {

unset($this->work_id, $this->seo_url, $this->active);

}

There's probably a better way to handle this ... but that's what comes to mind.
#5

[eluser]spmckee[/eluser]
Great D13, thanks!

I did have to change all the
Code:
if ($this->seo_url) {
parts to
Code:
if (isset($this->seo_url)) {
. Do you that's an issue?
#6

[eluser]Developer13[/eluser]
Nope, not an issue... actually it's proper Smile
#7

[eluser]spmckee[/eluser]
Right on. Thanks again!

Can't wait to see IT 1.0 Smile




Theme © iAndrew 2016 - Forum software by © MyBB