I'm trying to import a CSV File with Codeigniter, which contains the code below:
Voornaam;Achternaam;email
John;Doe;john.doe#gmail.com
Man;Made;man#made.com
The file is successfully uploaded, and when I print the code through PHP it indeed shows me the file contents in an array:
Array ( [0] => Array ( [Voornaam] => John [Achternaam] => Doe [email] => john.doe#gmail.com ) [1] => Array ( [Voornaam] => Man [Achternaam] => Made [email] => man#made.com ) )
The code is then processed through the following code:
foreach($csv_array as $row) {
$insertData = array(
'firstName' => $row['Voornaam'],
'lastName' => $row['Achternaam'],
'email' => $row['email']
);
};
Which unfortunately 'Undefined index: Voornaam' When i replace $row['Voornaam'] with '', the code processes fine, so lastName and email work. Now, I cannot for the life of me figure out why Voornaam is not processed, and how it may be fixed.
Related
In my ZF2 app I've got a form with several optional fields. If user decide to leave that field blank then it should set it as NULL in db, however - it doesn't work.
Example field:
$this->add(array(
'name' => 'productId',
'type' => 'select',
'options' => array(
'value_options' => array(
'global' => 'Global option', // this should be null
'mobile' => 'Another option'
)
),
));
And filter:
$inputFilter->add(array(
'name' => 'productId',
'validators' => array(
array(
'name' => 'Callback',
'options' => array(
// 'messages' => array(
// \Zend\Validator\Callback::INVALID_VALUE => $value,
// ),
'callback' => function ($value) {
if($value === 'global')
{
$value = null;
//return true;
// echo '<pre>' . var_export($value, true) . '</pre>';
// die();
}
}
),
),
),
));
This piece of code works when "mobile" option is selected. However, with 'global' option it doesn't work.
As you can see, I've done some basic debugging to make sure that value is overrided on callback and it indeed returns NULL. Regardless of that validator says:
array (
'productId' =>
array (
'callbackValue' => 'The input is not valid',
),
)
So I tried to return true on callback which resulted in below error:
Statement could not be executed (23000 - 1452 - Cannot add or update a child row: a foreign key constraint fails (app.codes, CONSTRAINT fk_products_id FOREIGN KEY (productId) REFERENCES products (id)))
How am I supposed to pass null param to my db? If I cut this field completly from form then it is ignored and everything works.
Found solution, it's pretty simple actually.
I can set desired select options to some specific value and then use ToNull filter to convert it into null -
https://framework.zend.com/manual/2.4/en/modules/zend.filter.null.html
Code for $inputFilter:
$inputFilter->add(array(
'name' => 'productId',
'required' => false,
'filters' => array(
array('name' => 'ToNull', 'options' => array('type' => \Zend\Filter\ToNull::TYPE_STRING)),
),
));
Using Moose, is it possible to create a builder that builds multiple attributes at once?
I have a project in which the object has several 'sets' of fields - if any member of the set is requested, I want to go ahead and populate them all. My assumption is that if I need the name, I'll also need the birthdate, and since they're in the same table, it's faster to get both in one query.
I'm not sure if my question is clear enough, but hopefully some sample code will make it clear.
What I have:
Package WidgetPerson;
use Moose;
has id => (is => 'ro', isa => 'Int' );
has name => (is => 'ro', lazy => 1, builder => '_build_name');
has birthdate => (is => 'ro', lazy => 1, builder => '_build_birthdate');
has address => (is => 'ro', lazy => 1, builder => '_build_address');
sub _build_name {
my $self = shift;
my ($name) = $dbh->selectrow_array("SELECT name FROM people WHERE id = ?", {}, $self->id);
return $name;
}
sub _build_birthdate {
my $self = shift;
my ($date) = $dbh->selectrow_array("SELECT birthdate FROM people WHERE id = ?", {}, $self->id);
return $date;
}
sub _build_address {
my $self = shift;
my ($date) = $dbh->selectrow_array("SELECT address FROM addresses WHERE person_id = ?", {}, $self->id);
return $date;
}
But what I want is:
has name => (is => 'ro', isa => 'Str', lazy => 1, builder => '_build_stuff');
has birthdate => (is => 'ro', isa => 'Date', lazy => 1, builder => '_build_stuff');
has address => (is => 'ro', isa => 'Address', lazy => 1, builder => '_build_address');
sub _build_stuff {
my $self = shift;
my ($name, $date) = $dbh->selectrow_array("SELECT name, birthdate FROM people WHERE id = ?", {}, $self->id);
$self->name($name);
$self->birthdate($date);
}
sub _build_address {
#same as before
}
What I do in this case, when I don't want to have a separate object as in Ether's answer, is have a lazily built attribute for the intermediate state. So, for example:
has raw_row => (is => 'ro', init_arg => undef, lazy => 1, builder => '_build_raw_row');
has birthdate => (is => 'ro', lazy => 1, builder => '_build_birthdate');
sub _build_raw_row {
$dbh->selectrow_hashref(...);
}
sub _build_birthdate {
my $self = shift;
return $self->raw_row->{birthdate};
}
Repeat the same pattern as birthdate for name, etc.
Reading any of the individual attributes will try to get data from raw_row, whose lazy builder will only run the SQL once. Since your attributes are all readonly, you don't have to worry about updating any object state if one of them changes.
This pattern is useful for things like XML documents, too -- the intermediate state you save can be e.g. a DOM, with individual attributes being lazily built from XPath expressions or what-have-you.
No, an attribute builder can only return one value at a time. You could build both by having each builder set the value of the other attribute before returning, but that gets ugly pretty quickly...
However, if you generally have two pieces of data that go together in some way (e.g. coming from the same DB query as in your case), you can store these values together in one attribute as an object:
has birth_info => (
is => 'ro', isa => 'MyApp::Data::BirthInfo',
lazy => 1,
default => sub {
MyApp::Data::BirthInfo->new(shift->some_id)
},
handles => [ qw(birthdate name) ],
);
package MyApp::Data::BirthInfo;
use Moose;
has some_id => (
is => 'ro', isa => 'Int',
trigger => sub {
# perhaps this object self-populates from the DB when you assign its id?
# or use some other mechanism to load the row in an ORMish way (perhaps BUILD)
}
);
has birthdate => (
is => 'ro', isa => 'Str',
);
has name => (
is => 'ro', isa => 'Str',
);
FACTS:
I am not very good at magento but have some experience working with API's using PHP.
TARGET:
What I actually want to do is
Download magento customer orders in our order system
Process the orders
Upload tracking code back to magento
Update magento products level based on the values fetched from our local Stock System running on a different server
ENVIRONMENT:
I am using a Login Plugin to make the store private so only customers with an account can access it.
PROBLEM:
IF 'AUTHENTICATION' PLUGIN IS DISABLED EVERYTHING WORKS FINE. BUT WHEN PLUGIN IS ENABLED STEP 3 AND 4 STOP WORKING.
ERROR SAMPLE:
Magento SOAP API V2 calls result in
SoapFault Object ( [message:protected] => looks like we got no XML document, when used for Private Sales store. I am able to login using API a/c,fetch orders but calls to update stock / upload tracking etc fail.
SAMPLE OUTPUT:
Login Successful [0a254c064fa033859bc75db94]
Request :
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento"><SOAP-ENV:Body><ns1:catalogInventoryStockItemUpdateRequestParam>sessionId>0a254c064fa033859bc75db94</sessionId><productId>1</productId><data><qty>100</qty><is_in_stock>1</is_in_stock></data></ns1:catalogInventoryStockItemUpdateRequestParam></SOAP-ENV:Body></SOAP-ENV:Envelope>
Result:
SoapFault Object ( [message:protected] => looks like we got no XML document [string:Exception:private] => [code:protected] => 0 [file:protected] => /var/www/vhosts/XXX/magentoAPI/stock.php [line:protected] => 31 [trace:Exception:private] => Array ( [0] => Array ( [file] => /var/www/vhosts/XXX/magentoAPI/stock.php [line] => 31 [function] => __call [class] => SoapClient [type] => -> [args] => Array ( [0] => catalogInventoryStockItemUpdate [1] => Array ( [0] => stdClass Object ( [sessionId] => 0a254c064fa033859bc75db94 [productId] => 1587 [data] => Array ( [qty] => 1000 [is_in_stock] => 1 ) ) ) ) ) [1] => Array ( [file] => /var/www/vhosts/XXX/magentoAPI/stock.php [line] => 31 [function] => catalogInventoryStockItemUpdate [class] => SoapClient [type] => -> [args] => Array ( [0] => stdClass Object ( [sessionId] => 0a254c064fa033859bc75db94 [productId] => 1587 [data] => Array ( [qty] => 1000 [is_in_stock] => 1 ) ) ) ) ) [previous:Exception:private] => [faultstring] => looks like we got no XML document [faultcode] => Client [faultcodens] => http://schemas.xmlsoap.org/soap/envelope/ )
I am new to CakePHP 2.0. It has too many changes from its previous version. I have many problems with implementing basic functionality in CakePHP 2.0. I did the same code in CakePHP 1.3 and 2.0 but the output is different so it creates a problem for me.
I have created one form as shown below:
<form name="User" method="post" action="http://192.168.1.24/project/api/documents/sub" ENCTYPE="multipart/form-data">
<table>
<tr><td><label>username:</label></td><td><input type="text" name="username"></td></tr>
<tr><td><label>password:</label></td><td><input type="text" name="password"></td></tr>
<tr><td><label>email:</label></td><td><input type="text" name="email"></td></tr>
<tr><td><label>Image:</label></td><td><input type="file" name="image"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit" />
I used this form for both my applications in CakePHP 1.3 and 2.0.
The controller code of CakePHP 1.3 is:
function api_sub()
{
$this->layout = false;
$this->data['Document'] = $this->params['form'];
pr($this->data); die();
}
The output of the above code is:
Array
(
[Document] => Array
(
[username] => mack
[password] => meack
[email] => mack#gmail.com
[submit] => Submit
[image] => Array
(
[name] => 01manta.jpg
[type] => image/jpeg
[tmp_name] => C:\wamp\tmp\phpF586.tmp
[error] => 0
[size] => 636306
)
)
)
The controller code in CakePHP 2.0 is as shown below:
public function api_sub()
{
//$this->layout = false;
$this->request->data['Document']= $this->request->data;
pr($this->request->data); die();
}
The output of the above code is as shown below:
Array
(
[username] => mack
[password] => meack
[email] => mack#gmail.com
[submit] => Submit
[User] => Array
(
[username] => mack
[password] => meack
[email] => mack#gmail.com
[submit] => Submit
)
)
Now the problem is that in CakePHP 2.0, the image array is missing in the output.
Can anyone tell me how I can get that image array in CakePHP 2.0? What changes do I have to do in my coding?
I could be because you're not using the FormHelper. Were you to use said helper the data would probably be properly populated in $this->data or $this->request->data. I can't find anything definitive on the matter the documentation though.
In any case, generic form data is always present in $this->request->params['form'], so the file data for your form would be in $this->request->params['form']['file'].
Instead of writing the HTML code for the form, you should do as follow:
echo $this->Form->create('ModelName');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('email');
echo $this->Form->input('image');
echo $this->Form->end('Submit!');
Note that in this example asumes you'll validate the image wihtin your model and it will probably be stored in your database.
Shoud you wish not to store the image in the database, you could replace the last input with:
echo $this->Form->input('image', array('type' => 'file'));
All of this will be accessible in your Controller through:
$this->request->data
This will be an array that should look like this:
array(
'ModelName' => array(
'username' => 'whatever_the_user_wrote',
'password'=> '********',
'email' => 'foo#bar.baz',
'image' => the image
)
)
Link to the Book: http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
Link to the API: http://api.cakephp.org/class/form-helper
I have a little problem.
There are two tables in my database: users and classes. The first one contains info about users, and the second one - about user classes and about access rights.
Now I'm extracting all data from there using this:
SELECT * FROM users NATURAL JOIN classes WHERE users.ID_User = '$id';
The mysql code works and returns a right array.
Now, when I'm doing next:
<?php
$result = mysql_query($sql_above);
$row = mysql_fetch_array($result);
print_r($row);
?>
... i'm getting this:
Array ( [0] => 1 [ID_User] => 1 [1] => John [Name] => John [2] => Doe [Surname] => Doe [3] => ... [16] => Owner [Class] => Owner [17] => [All] => [18] => [CanAuth] => [19] => [CanViewData] => [20] => [CanAddData] => [21] => [CanAlterData] => [22] => [CanDeactivateData] => [23] => [CanDeleteData] => [24] => [CanMgLocatari] => ... )
Columns from 17 till the end are access rights, noted in the database by 1 or 0. And there, they're missing.
Is there any option to enable in PHP configuration to correct this thing? Because on a Windows machine, the code executes properly and rights are working.
PHP Version 5.3.6-13ubuntu3.7 | Apache/2.2.20 (Ubuntu) | MySQL client version: 5.1.62
Please, reply. Thanks!