Welcome Guest, Not a member yet? Register   Sign In
DMZ - General question about $has_one and $has_many, and specific q regarding how to do something properly
#1

[eluser]lexusgs430[/eluser]
Hi,

First of all, been using DMZ since yesterday and it seems to be pretty damn amazing, so if whoever wrote and or took over the project reads this, much thanks, its helped me understand alot of things I havent been able to grasp regarding oop.

Ok so 2 questions. Ive set up my models, and ive set up the vars has one and has many on each model, I understand the concept of has one has many, but I dont exactly understand what setting that up enables me to do differently. That is vague but Im just going to move onto my main question because an answer to that might make me understand this....

So, Im working with items available for sale on different websites. I have a model for "items", and a model for "sites". (and more but keeping it simple)... - Simplified, it looks like this:

items - id, item, site
sites - id, site

So heres the thing. Im running a script to go fetch a large number of items at once, and for each item, store that item in the items table. With each run of the script, there may be 20 different sites, but also the same site may be repeated multiple times. -- So, first of all, I feel like instead of storing the site name in the items table, I should be storing it in the "sites table, and using site_id, in the items table correct?? But what I dont understand is, is this something the model is supposed to be aware of which will make it easier to do this? Or will I basically have to go: (within the for each loop) -- And im writing this out literally because Im bad with if else and formatting them properly so if you could show me how to write this properly if this is the way I need to do it, I would appreciate it very much --

Code:
$site = $siteNameReturnedFromScript;
$s = new Site();
if($s->site does not contain $siteNameReturnedFromScript then $s->save, else get $s->id where $s->id == $siteNameReturnedFromScript and $item->site_id = $s->id;

Anyways - Any help understanding the proper way to do this would be much appreciated!
#2

[eluser]lexusgs430[/eluser]
ok so I got it working, but is this the proper way of doing it (it seems like it but not sure) -- Also, I got stuck on this because of rogue characters breaking the save, so I fixed it with mysql_real_escape_string, but, didnt I read somewhere that CI automatically escapes the entries prior to inserting??

Code:
in my items model I have this:


$s = new Site();
$item->site_id = $s->_get_site_id($site);


in my site model this:


   public function _get_site_id($site)
    {

        $s = new Site();
        if ($s->get_by_site($site)->exists())
        {
            return $s->id;
        }
        else
        {
            $s->site = $site;
            $s->save();
            return $s->id;
        }

    }




Theme © iAndrew 2016 - Forum software by © MyBB