Welcome Guest, Not a member yet? Register   Sign In
faster,better way to get stuff from db + add/edit theory
#11

[eluser]Hannes Nevalainen[/eluser]
$this->db->query() in the User Guide
Take a look under "Query Bindings".

=)
#12

[eluser]codex[/eluser]
[quote author="Hannes Nevalainen" date="1217555145"]$this->db->query() in the User Guide
Take a look under "Query Bindings".

=)[/quote]

Hmm, indeed. Though I'm in the userguide pretty much every day, I failed to see this. :-)
#13

[eluser]InterCoder[/eluser]
I have a page with a controller called: comments and I also want to improve it's structure.

It's a page in my admin control panel and it has the following actions: add, edit, view, delete. Now, I'm not quite sure if IgnitedRecord is suitable for me..?
#14

[eluser]sophistry[/eluser]
IgnitedRecord puts a layer of abstraction between the code and the db. It's basically a "super model" that gives you some handy ways of interacting with your db and will help cut down on redundant code. The approach is called Object Relational Mapping (ORM).

It's a great tool if you've started to get annoyed at the repetitive nature of coding models and you want something to help distill that away.

BTW, don't use the word view as a method - it's reserved (in PHP 4 only - in PHP 5 it's ok): reserved names

cheers.
#15

[eluser]InterCoder[/eluser]
I'll check out the manual from IgnitedRecord, but still don't see the advantages of it. Can't you give me an example with the "old"-way and the "IgnitedRecord"-way?
#16

[eluser]sophistry[/eluser]
nope. ;-)

i assume you already know the "old" way because that is just the CI way and it's in the docs. the "new" way via IgnitedRecord is one of the most thoroughly documented pieces of third party CI contributions i've seen so you should have no trouble using it.

but, check out the IgnitedRecord thread and you'll see.

advantages are that you streamline your controller and model code for interactions with a database. if you aren't using a database or you just use the database to store very simple stuff then you probably won't see the benefit. but, if you have many db tables with joins and related tables you'll see why ignitedrecord is useful.
#17

[eluser]neen[/eluser]
I have the following save() function in my category controller:
Code:
public function save()
        {
            // load the validation library
            $this->load->library('validation');
            // set the validation rules
            $rules['name'] = 'required';
            $this->validation->set_rules($rules);
            // validate the form...
            // ...does it validate?
            if($this->validation->run() == FALSE)
            {
                $this->session->set_flashdata('error', 'The category name is required!<br>Please make sure it is filled in.');
                redirect('categories/' . $this->session->userdata('return_url'), 'location');
            } else {
                // save the edited category
                // $cat = $this->category_model->save($this->input->post, $this->input->post('id'));
                $cat = 1;
                $cat = $this->category_model->getCatName($cat);
                $return_url = $this->session->userdata('return_url');
                // is the ID var set in POST? if it isn't, show the return link, if it is, just show the category list
                $this->success = $this->input->post('id') ? "The $cat category was saved!"
                : "The $cat category was saved!<br><a >Return</a>";
                 $this->session->set_flashdata('success', $this->success);
                $this->session->set_userdata('redirected', true);
                redirect('categories/' . $this->session->userdata('on_save'), 'location');    
            }
        }
and the following is in my category_model:
Code:
public function save($data, $id = NULL)
        {
            // we are adding a new category
            if($id === NULL)
            {
                // subcat is not set
                if($data['subcat'] == 0 || !isset($data['subcat']))
                {
                    // the parent_id of the new category is the value of parentCategory
                    $data['parent_id'] = $data['parentCategory'];
                // subcat is set
                } else {
                    // parent_id is the id of the subcategory
                    $data['parent_id'] = $data['subcat'];
                
                }
            
                if($data['parent_id'] == 0)
                {
                    // there is no parent_id (this is a top-level category)
                    $data['parentCategory'] = NULL;
                
                } else {
                    // get the data for the parent category
                    $data['parentCategory'] = $this->getCatbyID($data['parent_id']);
                }

                // remove the parent category name & space from the URL ("naruto/naruto characters" becomes "naruto/characters")
                $url = url_title(str_replace($data['parentCategory']['name'] . " ", '', $data['name']), 'dash');
                // insert the url
                $inserturl = array( 'url' => $url, 'parent_id' => $data['parentCategory']['url_id'] );
                $data['url_id'] = $this->url_model->saveURL($inserturl);
                // unset the unwanted data (if we leave these in, it will cause an error upon insert)
                unset($data['parentCategory'], $data['subcat'], $data['parentCategory_name']);
                // insert the category into the categories table
                $this->db->insert('categories', $data);

                // return the id of the row that was inserted
                return $this->db->insert_id();
            } else {
                // we are updating an existing category
                // get the current category
                $category = $this->db->get_where('categories', array('id' => $id));
                $category = $category->result_array();
                
                
                
                $this->db->update('categories', $data);
                
            }
        }




Theme © iAndrew 2016 - Forum software by © MyBB