Welcome Guest, Not a member yet? Register   Sign In
[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition)

[eluser]bEz[/eluser]
@phil
Although I have tested the "beta-1" example application, this "beta-2" update will allow me to "go deeper" Tongue with a more practical example.
It sounds like I asked for a feature in the nick of time.

I did want to ask you about an error during the "init" of the example app.

Creating table bugs...
Code:
A PHP Error was encountered

Severity: Notice

Message: ob_flush() [ref.outcontrol]: failed to flush buffer. No buffer to flush.

Filename: controllers/admin.php

Line Number: 146

Probably not related to the DMZ library, but just in case.

Anyhow, off to the Developer's Workshop! Smile

[eluser]OverZealous[/eluser]
Huh, that's weird. I've never seen PHP throw an error with an empty buffer. It might be possible to surround the ob_flush in question with
Code:
if(ob_get_level()) {
    ob_flush();
}

Or simply disable it. The purpose of the flush in the admin controller is to stream the output, so you can see the tables as they are created.

[eluser]OverZealous[/eluser]
Post Removed: 1.4.0 was released

[eluser]ntheorist[/eluser]
one quick idea to automate the regeneration of the production cache files would be to run filemtime() on the model class file itself and compare it to the cache file's fliemtime. If it's older than the model file, regenerate it.

you could pass the $file var in the autoload functions to Datamapper just before require_once, so when the model loads it already has its file location available. Then just add to line 131
Code:
// If you edit and save the model file, it will be newer than the cache file and force regeneration
if(file_exists($cache_file) && (filemtime($cache_file) < filemtime($classpath)) )

this way the production cache will constantly keep up with model edits and help to prevent data corruption as warned in the docs.

n

[eluser]Ninja[/eluser]
I cant seem to get not or '!=" to work everything works fine for including but never for excluding
here are two diff attempts at a foreach loop that is populated from the database but needs to populate excluding companies that are already clients. Please help.
Code:
$client = new Client();
            $client->get('id');    
                
            foreach ($client->all as $entry)
            {
                $clientID = $entry->company_id;
                
                $comp = new Company();
                
                $comp->where_not_in('id', $clientID)->get();
                foreach($comp->all as $entry)
                {
                $data['comp'][$entry->id] = $entry->companyname;
                }
            }

I tried the same thing using '!='
Code:
$comp->where('id !=', $clientID)->get();

When i remove the not or the != it selects all existing clients but why cant i exclude all existing clients? I dont understand.

[eluser]OverZealous[/eluser]
@ntheorist
Even if I do that, it won't help if only the database changes (which is unlikely). But, like you said, it should prevent data corruption.

Ninja
That's not SQL. In SQL to specify !=, you use <>. There is no "!" operator. You can use the NOT keyword to negate a group of tests, as well.

[eluser]naren_nag[/eluser]
Downloading, will be using it in a new version of the app I'm working on. Will report back.

Great Job!

Naren

[eluser]ntheorist[/eluser]
yeah that's true, if you changed your db table without modifying the model file it could still cause probs i guess, but still would provide a bit of self cleanup in many cases. I'm going to try to implement it and see how well it works.

Hey also btw, while sql does use '<>' as a NOT operator, it most certainly does allow for '!='

It appears on the bottom of the wiki page (abv External Links), MySql's list of operators and even Codeigniter's own active record docs (point 2 under $this->db->where). I'm only referring to mysql but regardless i'd guess the drivers should work it out.

however, there's a few things wrong with Ninja's code

He seems to be using a self join for Clients, but he's querying against EVERY individual id in the clients_companies table. where_not_in can and should take an array of data. Also $client->get('id') does nothing, with get() you specify limit, offset, not fields - AR should strip it out but still its misleading.

@Ninja, try this perhaps

Code:
$client = new Client();
$client->select('company_id');
$client->get();    

$client_company_ids = array();

foreach ($client->all as $entry)
{
       $client_company_ids[] = $entry->company_id;
}
          
$comp = new Company();
                
$comp->where_not_in('id', $client_company_ids)->get();

foreach($comp->all as $entry)
{
       $data['comp'][$entry->id] = $entry->companyname;
}


Note: i don't know exactly how your tables are setup, but if you ARE using a self join table you may need to use something like $client->select('clients_companies.company_id'). The point is you don't want to waste time getting every column when you just want the id but i think that's what you meant by get('id');

n

[eluser]Ninja[/eluser]
Firstly Thanx guys i really appreciate all the assistance
@ ntheorist

lol just for the record Im not a he... (Yip female developer)

Thanx for the code it works but not entirely i think the scope might be a bit off because it only excludes the last client in the array instead of all of them. thats why i had the company foreach loop nested.

I am using in-table foreign keys. company_id is a column in the clients table. The idea was to have companies as an abstract class and have clients, manufacturers and suppliers inherit from it, as i felt this would reduce redundancy. I love the use of in table foreign keys as it makes things so much simpler and im willing to change anything to avoid using joining tables.

[eluser]ntheorist[/eluser]
pardon the mistake, i don't meet very many ninja girl programmers Tongue

anyway, i think i figured out a solution. Instead of getting all the company_ids from clients then cross referencing them again against the companies table (which requires a few queries) you can do it with a join and get records where the client id is null (thus, not a client)

i'm assuming since you're not using join tables, then datamappers relationship functions wont work for you. If you did, it would be as such
Code:
$comp = new Company();
$comp->where_related_client('id IS NULL')->get();

i tested this to get users in my own db who lacked roles. actually kinda a neat trick im glad u brought it up.

In your case you will probably want to use active record.
Code:
$comp = new Company();
$comp->select('*');
$comp->db->join('clients','clients.company_id = companies.id');
$comp->db->where('clients.id IS NULL');
$comp->get();

to be honest i can't promise mixing AR with Datamapper will not mess up anything but let me know if that works for you.

I feel you about not wanting to create relationship tables for everything, in some cases it can seem overkill. DM is designed to use them and provides handy functions based on them, but having too many can be a pia to manage.

cheers,

n




Theme © iAndrew 2016 - Forum software by © MyBB