Little ORM help

Here's the code, I think there's a slight problem with the code.

Here's what I get in error:
array_shift() [function.array-shift]: The argument should be an array

On this line:
array_shift($data);
$original = $_POST['original'];
if($master = MyActiveRecord::FindById('widgets',$original)){
        $data = get_object_vars($original);
        array_shift($data);
        $new = MyActiveRecord::Create('clones',$data);
        $new->save();
        if(false === $new->get_errors()){
                header('Content-type: text/html; charset=utf-8');
                //this is the part you need:
                print 'item_' .  $new->id;
        }else{
                header('HTTP/1.0 500 Server Error',true,500);
                exit;
        }
}else{
 
        header('HTTP/1.0 404 Missing',true,404);
 
}

Open in new window

LVL 4
TheYanAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Beverley PortlockConnect With a Mentor Commented:
Without seeing the context into which that program fragment fits it is impossible to say. These sort of challenges often depend on setting up obscure flaws that very rarely occur in general programming work.

From reading the comment above I would *guess* that he wants you to use a different variable than $original and since the only variable on line 2 that is assigned a value is $master it may be that variable that he is hinting at, but without knowing what the class method returns you can only guess.
0
 
Beverley PortlockCommented:
The variable data must not contain an array in that case. It may be that some situations return results in which no array exists in which case you could alter your code like so

        $data = get_object_vars($original);

        if ( is_array($data) )
            array_shift($data);
           
0
 
TheYanAuthor Commented:
Sorry, the problem isn't there. There's something wrong with the line 2 and 3.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Beverley PortlockCommented:
Given the fact that the error code you mentioned explicitly mentions an array problem I cannot see how the earlier lines would cause a difficulty. Still I assume that we are now talking about these lines

$master = MyActiveRecord::FindById('widgets',$original)
$data 
=
 get_object_vars
(
$original
);

Open in new window

and there is a raft of things that could be wrong with it -
  1. The class MyActiveRecord has not been defined or has been spelt differently from how it is used here
  2. The class MyActiveRecord is defined but has not had its source included
  3. The method FindById is absent in the class or is spelt differently
  4. The class has been used dynamically rather than statically $MyActiveRecord = new MyActiveRecordClass
  5. get_object_vars may only return a single value and not an array (seems most likely to me)
It could be any of that lot. Try adding

error_reporting(E_ALL);

to the start of the script and see if you get a more informative error message. If so then post it up as it will be a help in solving this.


 
0
 
Beverley PortlockCommented:
Hmm - the editor seems to have stuffed my copy of lines 2 & 3......
0
 
TheYanAuthor Commented:
"Read through the second and third lines slowly and carefully. Look at  
each variable that is being set and each variable that is being read.  
(Variables to the left of the = are being set.) Ask yourself what it's  
supposed to be carrying, and see if you can spot what it really is  
instead.

Here's a hint: In line two, I assigned a value to a variable, but on  
line 3, I forgot to use it and used something else instead. "
0
 
Beverley PortlockCommented:
I'm confused by your last post. Are you telling me that you solved it? You specifically asked about the array_shift and $data and I gave you some tips on that so I do not see what you are getting at. There is a POTENTIAL error on line 2 where it says

if ($master = MyActiveRecord::FindById('widgets',$original) )

and it might have been intended to say

if ($master == MyActiveRecord::FindById('widgets',$original) )



however it should be noted that doing assignments in IF statements is quite common, for instance a statement like

if ( $fp = fopen("filename","r") .....

is a very common occurrence and is not incorrect so the statement in line 2 might be correct.


0
 
TheYanAuthor Commented:
No that didn't solved the problem, try read my last post if you follow it you'll find an error. I can't find it.

"Read through the second and third lines slowly and carefully. Look at  
each variable that is being set and each variable that is being read.  
(Variables to the left of the = are being set.) Ask yourself what it's  
supposed to be carrying, and see if you can spot what it really is  
instead.

Here's a hint: In line two, I assigned a value to a variable, but on  
line 3, I forgot to use it and used something else instead. "
$original = $_POST['original'];
if($master = MyActiveRecord::FindById('widgets',$original)){
        $data = get_object_vars($original);
        array_shift($data);
        $new = MyActiveRecord::Create('clones',$data);
        $new->save();
        if(false === $new->get_errors()){
                header('Content-type: text/html; charset=utf-8');
                //this is the part you need:
                print 'item_' .  $new->id;
        }else{
                header('HTTP/1.0 500 Server Error',true,500);
                exit;
        }
}else{
 
        header('HTTP/1.0 404 Missing',true,404);
 
}

Open in new window

0
 
Beverley PortlockCommented:
Syntactically there is nothing wrong. The only question is whether the IF on line 2 should be an assignment (unlikely, but possible) or a comparison (more likely). Without knowing the context that the code fragment is used in it is not possible to deduce the values and types of the variables used.

Has the error message changed? Is it still complaining about $data? If so then $data is not an array and it has to be for array_shift to work.

What are these rather cryptic paragraphs you keep posting? Are they from someone else looking at this?
0
 
TheYanAuthor Commented:
There's a person who keep challenging me: "In line two, I assigned a value to a variable, but on  
line 3, I forgot to use it and used something else instead."

I have no idea what it's all about, isn't there any problems in variables with those 2 lines?
0
 
thinkingman2Connect With a Mentor Commented:
This may be pointless by now, but I'm thinking this is the situation:

if($master = MyActiveRecord::FindById('widgets',$original)){
        $data = get_object_vars($original);
        array_shift($data);
        $new = MyActiveRecord::Create('clones',$data);

Master IS a value assignment, so IF the assignment succeeeds (i.e. $master now contains an object), then the next line should read:

$data = get_object_vars($master);

If the TheYan plugs this into their code and it works, that may be the answer. Otherwise, that means there's all kinds of other problems with this code...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.