Solved

Little ORM help

Posted on 2009-07-02
11
232 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 100 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 25 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

896 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now