joelwan / php-object-generator

ORM class generator for PHP
http://www.phpobjectgenerator.com
29 stars 30 forks source link

attribute name not equal to parent name issue #7

Closed Rafeethu closed 10 years ago

Rafeethu commented 12 years ago

hey guys,

when i create an object which has two attributes with different names which refer the same parent type, one attribute name has to be different than the parent name. this is creating an issue when we call object->GetAttribut2 function.

is there a workaround to this?

Rgds Rifky

Loksly commented 12 years ago

you mean a class like this one:

http://www.phpobjectgenerator.com/?language=php5&wrapper=pog&objectName=treenode&attributeList=array+%28%0A++0+%3D%3E+%27data%27%2C%0A++1+%3D%3E+%27nextNode%27%2C%0A++2+%3D%3E+%27child%27%2C%0A%29&typeList=array+%28%0A++0+%3D%3E+%27VARCHAR%28255%29%27%2C%0A++1+%3D%3E+%27BELONGSTO%27%2C%0A++2+%3D%3E+%27BELONGSTO%27%2C%0A%29

does it fail when you call

$object = new treenode(); [...] $child = $object->GetChild();

Rafeethu commented 12 years ago

yes. a simple example is

http://www.phpobjectgenerator.com/?language=php5.1&wrapper=pdo&pdoDriver=mysql&objectName=Invoice&attributeList=array+%28%0A++0+%3D%3E+%27customer%27%2C%0A++1+%3D%3E+%27address%27%2C%0A++2+%3D%3E+%27customer_address%27%2C%0A++3+%3D%3E+%27shipping_address%27%2C%0A%29&typeList=array%2B%2528%250A%2B%2B0%2B%253D%253E%2B%2527VARCHAR%2528255%2529%2527%252C%250A%2B%2B1%2B%253D%253E%2B%2527BELONGSTO%2527%252C%250A%2B%2B2%2B%253D%253E%2B%2527BELONGSTO%2527%252C%250A%2B%2B3%2B%253D%253E%2B%2527BELONGSTO%2527%252C%250A%2529

here address, customer_address, shipping_address are all child of address class

$inv = new Invoice(); [...] $inv_address = $inv->GetAddress(); <-------- this will work $inv_shippingaddress = $inv->GetShipping_address(); <-------- this is not working...

i think if we can mention the parent or child original class name and then generate objects will solve the issue.

is the same error occuring for you?

Loksly commented 12 years ago

You are right it won't work as the generator generates code like these:

/**

which should be :

/**

There should be a way to choose the class for these fields. The generator doesn't have enough information. I'll see if I can add that funcionality as soon as possible. Change the code of the class by yourself meanwhile.

Have a nice day!

Rafeethu commented 12 years ago

i have changed the coding on my local server and it's working now.. i'll try to upload it to git asap so that you can review it. i mean the pog code.

Rafeethu commented 12 years ago

Loksly,

i have changed the coding to accept parent object name. can you revise and see whether it's ok.

mean time any idea of incorporating prepared statements in to pog? is using prepared statements rather than direct script will cause performance issue in pog?

rgds Rifky

Loksly commented 12 years ago

I've read the code right now. I need more time for a further review. I've seen you've change the wsdl for adding a new parameter (classlist), that breaks backwards compatibility and I think joel should be the one who has to be agree with that before accepting the commit. I agree with you with the use of jquery. It works fine and is expected to working fine in the future. About using prepared statements there are some issues. There should be a way to choose if you want the generated code to use them or not. It's easy to generate code for a Get, Insert and Delete functions and harder for a GetList or for a DeleteList functions. I think is a mistake to break compatibility with code already generated. Imaging a code like this:

$restrictions = array();
$restrictions[] = array( 'year','<',date('Y') );
$restrictions[] = array( ' and exists (select * invoices where  invoices.`deleted`=0 and invoices.project=project.projectId )' );
$projects = $project->GetList($restrictions,'title');

What kind of code for GetList do you suggest? This kind?

(GetList function)
...
$counter = 0;
$params = array();

for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
{
    if (sizeof($fcv_array[$i]) == 1)
    {
        $this->pog_query .= " ".$fcv_array[$i][0]." ";
        continue;
    }
    else
    {
        if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
        {
            $this->pog_query .= " AND ";
        }
        if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
        {
            if ($GLOBALS['configuration']['db_encoding'] == 1)
            {
                $value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
                $this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
            }
            else
            {
                if (POG_Base::IsColumn($fcv_array[$i][2]))
                {
                    $value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
                    $this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
                }else{
                    $value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
                    $this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." :param".$counter;
                    $params[':param'.$counter] = $value;

                    $counter++;
                }
            }
        }
        else
        {
            $value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
            $this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
        }
    }
}

if (counter>0)
{
    $preparedStatement = Database::PreparedStatement($this->pog_query);
    foreach($params as $name => $val)
        $preparedStatement->bindParam($name,$val);
    $preparedStatement->execute()
}else{
//current code
}

//Note: I've written this code here (& without any test)

As I have said before, we have to see if the new code works properly with the one users of pog might have written. About performance I've read is not very appreciable. Better table/database design increases performance more significantly. I guess security (SQL injection prevention) is a better reason for using prepared statements.

joelwan commented 12 years ago

I think running the existing unit tests as well as creating new ones to validate backward compatibility of existing plugins should be a pre-requisite before code can be merged. Thoughts?

Loksly commented 12 years ago

You are right, I'm sorry. I did the commit/merge using the mobile phone. I'll check the code tonight (in about 5 hours). I already read it and it looks like it works. I confess I haven't run the tests as I should.

So sorry.