Solved

Little ORM help

Posted on 2009-07-02
11
235 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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