?
Solved

Little ORM help

Posted on 2009-07-02
11
Medium Priority
?
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

752 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