Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Little ORM help

Posted on 2009-07-02
11
Medium Priority
?
241 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:TheYan
  • 6
  • 4
11 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24761754
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
 
LVL 4

Author Comment

by:TheYan
ID: 24762963
Sorry, the problem isn't there. There's something wrong with the line 2 and 3.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24763308
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24763313
Hmm - the editor seems to have stuffed my copy of lines 2 & 3......
0
 
LVL 4

Author Comment

by:TheYan
ID: 24763497
"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
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24764814
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
 
LVL 4

Author Comment

by:TheYan
ID: 24764911
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
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24765291
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
 
LVL 4

Author Comment

by:TheYan
ID: 24765339
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
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 300 total points
ID: 24767544
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
 
LVL 2

Assisted Solution

by:thinkingman2
thinkingman2 earned 75 total points
ID: 24767941
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question