Monday, November 30, 2015

Re: Retrieving data from blob data type - cakephp 3

Try this : 

http://stackoverflow.com/questions/32275558/upload-files-using-cakephp-3-and-store-it-in-a-blob

On Sunday, May 10, 2015 at 8:46:54 AM UTC+5:30, Ryan de Haast wrote:
Hi All

Can anybody explain to me how to get the data from column type that is a blob. when I retrieve the data from the database I'm able to get the details for all the columns however when a column is a blob it returns a resource. When I do a vardump on the data that's returned it shows it as resource stream

Here's some of my code, the part where I'm trying to get the contents:
foreach($list as $anArt)
{
 
pd($anArt);
  print_r($anArt->article_serialized);
  var_dump($anArt->article_serialized);
  $contents = fread($anArt->article_serialized, 1000000);
  pd($contents);
  $data = "";
  while (!feof($anArt->article_serialized)) {
   
$data .= stream_get_line($anArt->article_serialized, 1000000);
  }
 
pd($data);
}

The pd function is debug function used in the above code is just to print out the data as I'm working through it. The data in the specific column is stored as a serialized string

Please advise...

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Sunday, November 29, 2015

CakePHP 3.1.5 Released

The CakePHP core team is happy to announce the immediate availability of CakePHP 3.1.5. This is a maintenance release for the 3.1 branch that fixes several community reported issues.

Bugfixes
--------

You can expect the following changes in 3.1.5. See the [changelog](http://cakephp.org/changelogs/3.1.5) for every commit.

* `Collection::toArray()` now drops the keys when dealing with RecursiveIterator. This makes the collection features easier to reason about as they generally do what people have been expecting.
* Improved API documentation.
* `ResultSet::isEmpty()` no longer consumes records on buffered iterators that had not already been iterated.
* The ORM no longer emits invalid queries when eager loading associations using the subquery strategy, and using an ORDER BY clause. Fields used in ORDER BY clauses are also included in the selected fields.
* Error handling is now compatible with PHP7. This removes the last known compatibility error with PHP7.
* BelongsToMany associations use `bindingKey` correctly now.
* Integer marshalling correctly accepts negative values now.
* When executing Shell 'main' method the current command name is set to 'main'.
* spellcheck is now a standard attribute. Unlike most HTML5 attributes, spellcheck requires 'true' and 'false' values, which means it cannot be a minimized attribute.
* CSRF validation is applied to all HTTP methods that are not 'GET', 'OPTIONS' or 'HEAD'. This prevents invalid HTTP methods from bypassing CSRF validation.
* `RouterBuilder::resources()` correctly inflects the object id when using `'inflect' => 'dasherize'` and nested resources.
* `TimeHelper::format()` no longer shifts string datetimes to the supplied timezone before formatting.
* `Shell::createFile()` no longer converts `\n` to `\r\n` silently when running on windows.

Minor Enhancements
------------------

* `RouteBuilder::addExtension()` was added. This method lets you incrementally add extensions instead of replacing the connected extensions.
* The options passed to `Table::save()`, and `HasMany`/`BelongsToMany` link/unlink/replace methods are being passed through to the internal `Table::save/delete()` calls.
* The `CsrfComponent` now supports an `httpOnly` option. Enabling this option makes the CSRF cookie inaccessible to client side scripting.

As always, a huge thanks to all the community members that helped make this release happen by reporting issues and sending pull requests.

Download a [packaged release on github](https://github.com/cakephp/cakephp/releases).

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Scaffolding Composite Keys

How can I solve composite keys , i want create am application using scaffolding

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

CakePHP 2.x Persistent virtual fields problem

I have a brainteaser about virtual fields on associated models. I know it has it's limitations as you can read here: http://book.cakephp.org/2.0/en/models/virtual-fields.html#limitations-of-virtualfields
But in my specific case, I can't apply the theory of passing the virtualField property from one model to another (even when I read this topic: http://stackoverflow.com/questions/14630819/using-virtual-fields-in-cakephp-2-x).

I'll try to explane my case as clear as possible.

Database info

I have 3 tables to make a navigation on my website:

menus
id
name

pages
id
title
content

menu_page_links
id
menu_id
page_id
title
lft
rgt
plugin
controller
action

One page can be added to multiple menus, that's why I've made a link table (menu_page_links) between menus and pages.

Application info

Menu model

class Menu extends AppModel
{
   
public $hasMany = array(
       
'MenuPageLink' => array(
           
'className' => 'MenuPageLink',
           
'foreignKey' => 'menu_id'
       
)
   
);
}

Page model

class Page extends AppModel
{
   
public $hasMany = array(
       
'MenuPageLink' => array(
           
'className' => 'MenuPageLink',
           
'foreignKey' => 'page_id'
       
)
   
);
}

MenuPageLink model

class MenuPageLink extends AppModel
{
   
public $belongsTo = array(
       
'Menu',
       
'Page'
   
);

   
// to calculate the depth of every menu page link

   
public $virtualFields = array(
       
'depth' => 'COUNT(MenuPageLink.title) - 1'
   
);
}

AppController

Now in my application, I want to load a specific menu on every page, e.g. the "Primary menu" from the "menus" table.
So in AppController.php, I referred to the tree needed models ("Menu", "Page" and "MenuPageLink") as following:

public $uses = array(
   
'Menu',
   
'Page',
   
'MenuPageLink'
);

Then I get my menu pages that belongs to my specific menu with id "2":

$menu = $this->MenuPageLink->find('all', array(
   
'fields' => array(
       
'MenuPageLink.id',
       
'MenuPageLink.title',
       
'MenuPageLink.plugin',
       
'MenuPageLink.controller',
       
'MenuPageLink.action',
       
'MenuPageLink.depth'
   
),
   
'joins' => array(
        array
(
           
'table' => $this->MenuPageLink->table,
           
'alias' => 'Parent',
           
'type' => 'LEFT',
           
'conditions' => array(
               
'MenuPageLink.lft BETWEEN Parent.lft AND Parent.rgt',
               
'MenuPageLink.menu_id' => 1
           
)
       
)
   
),
   
'conditions' => array(
       
'MenuPageLink.menu_id' => 2,
       
'MenuPageLink.lft >' => 1,
       
'MenuPageLink.deleted' => null
   
),
   
'group' => 'MenuPageLink.id',
   
'order' => array(
       
'MenuPageLink.lft ASC'
   
)
));

This is my result when I debug my $menu variable:

array(
(int) 0 => array( 'MenuPageLink' => array( 'id' => '36', 'title' => 'Home', 'plugin' => '', 'controller' => 'home', 'action' => 'index', 'depth' => '1' ) ),
(int) 1 => array( 'MenuPageLink' => array( 'id' => '39', 'title' => 'News', 'plugin' => '', 'controller' => 'news_articles', 'action' => 'index', 'depth' => '1' ) ),
(int) 2 => array( 'MenuPageLink' => array( 'id' => '37', 'title' => 'About the park', 'plugin' => '', 'controller' => 'pages', 'action' => 'view', 'depth' => '1' ) ),
(int) 3 => array( 'MenuPageLink' => array( 'id' => '41', 'title' => 'Attractions', 'plugin' => '', 'controller' => 'attractions', 'action' => 'index', 'depth' => '2' ) ),
(int) 4 => array( 'MenuPageLink' => array( 'id' => '42', 'title' => 'Animals', 'plugin' => '', 'controller' => 'animals', 'action' => 'index', 'depth' => '2' ) ),
(int) 5 => array( 'MenuPageLink' => array( 'id' => '43', 'title' => 'Events', 'plugin' => '', 'controller' => 'events', 'action' => 'index', 'depth' => '2' ) ),
(int) 6 => array( 'MenuPageLink' => array( 'id' => '44', 'title' => 'Shows', 'plugin' => '', 'controller' => 'shows', 'action' => 'index', 'depth' => '2' ) ),
(int) 7 => array( 'MenuPageLink' => array( 'id' => '45', 'title' => 'History', 'plugin' => '', 'controller' => 'pages', 'action' => 'history', 'depth' => '2' ) ),
(int) 8 => array( 'MenuPageLink' => array( 'id' => '38', 'title' => 'Info', 'plugin' => '', 'controller' => 'pages', 'action' => 'view2', 'depth' => '1' ) ),
(int) 9 => array( 'MenuPageLink' => array( 'id' => '40', 'title' => 'Media', 'plugin' => '', 'controller' => 'pages', 'action' => 'media', 'depth' => '1' ) )
)

So far so good!

What I'm trying

And here comes my problem. I only want to select the pages where depth = 1. I tried to make a new "conditions" parameter:



$menu
= $this->MenuPageLink->find('all', array(
   
'fields' => array(
       
'MenuPageLink.id',
       
'MenuPageLink.title',
       
'MenuPageLink.plugin',
       
'MenuPageLink.controller',
       
'MenuPageLink.action',
       
'MenuPageLink.depth'
   
),
   
'joins' => array(
        array
(
           
'table' => $this->MenuPageLink->table,
           
'alias' => 'Parent',
           
'type' => 'LEFT',
           
'conditions' => array(
               
'MenuPageLink.lft BETWEEN Parent.lft AND Parent.rgt',
               
'MenuPageLink.menu_id' => 1
           
)
       
)
   
),
   
'conditions' => array(
       
'MenuPageLink.menu_id' => 2,
       
'MenuPageLink.lft >' => 1,
       
'MenuPageLink.deleted' => null,
       
'MenuPageLink.depth' => 1 // <-- new rule in my query
   
),
   
'group' => 'MenuPageLink.id',
   
'order' => array(
       
'MenuPageLink.lft ASC'
   
)
));

and after a lot of headaches to make this work, I even tried to use the Model::query() method:

$this->MenuPageLink->query(
   
"SELECT
        `MenuPageLink`.`id`
        , `MenuPageLink`.`title`
        , `MenuPageLink`.`lft`
        , `MenuPageLink`.`rgt`
        , `MenuPageLink`.`show`
        , `MenuPageLink`.`deleted`
        , (COUNT(`MenuPageLink`.`title`) - 1) AS `MenuPageLink`.`depth`
    FROM
        `blwfun`.`menu_page_links` AS `MenuPageLink`
    LEFT JOIN
        `blwfun`.`menus` AS `Menu`
    ON
        (`MenuPageLink`.`menu_id` = `Menu`.`id`)
    LEFT JOIN
        `blwfun`.`pages` AS `Page`
    ON
        (`MenuPageLink`.`page_id` = `Page`.`id`)
    LEFT JOIN
        `blwfun`.`menu_page_links` AS `Parent`
    ON
        (`MenuPageLink`.`lft` BETWEEN `Parent`.`lft` AND `Parent`.`rgt` AND `MenuPageLink`.`menu_id` = 1)
    WHERE
        `Parent`.`menu_id` = 1 AND `MenuPageLink`.`lft` > 1 AND `MenuPageLink`.`deleted` IS NULL
AND `MenuPageLink`.`depth` = 1
    GROUP BY
        `MenuPageLink`.`id`
    ORDER BY
        `MenuPageLink`.`lft` ASC"

));

I keep getting the sql error, but I don't know how to solve it. As I said, I don't know how to apply the theory in de CakePHP docs (http://book.cakephp.org/2.0/en/models/virtual-fields.html#limitations-of-virtualfields) to my specific case.
Is there anybody who can help me out with this please?

You would be a hero to me :)

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Saturday, November 28, 2015

Re: Displaying, validating and saving multiple models in a form


Sorry, I just realised that I was wrong in that it DOESN'T actually try to validate the child fields when there is just one child. Apologies for that. 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Displaying, validating and saving multiple models in a form

Hi,

I'm working on an event management system for a small educational company, that's turned out to be more complex than expected (isn't that always the way?).

Basically, people apply online to book their child/children on a course instance. They initially state how many child places they want to book, then the form generates the appropriate number of field sets for the number of children, creating them using a count and naming the fields 'Kids.X.fieldname' based on that count.

The form is multi-model - saving the booking details, the parents details and the child details separately. A 'User' account is created from the parents details, 'Kids' are owned by 'Users' and I've set it up so that 'Bookings' are owned by 'Users' (and the course 'Instances' they are related to) but also have a HABTM relationship with 'Kids'. I've done it like this so that in Users can add individual kids to any future bookings (they have a lot of people coming back for more).

It seems to (mostly) work okay for when one child is selected - it validates the data entered against the appropriate model, including the 'Kid', processes the form and writes the data to the respective tables - the only glitch being it doesn't seem to create the 'Kid' though does add a phantom 'kid_id' to the 'bookings_kids' table!

However, it works even less well for when there are multiple kids. It processes the form but doesn't validate the multiple 'Kids' fields (even though the form is still indicating that they are required by putting labels in bold etc) and doesn't create the 'Kids' data or the add the relationship to 'bookings_kids'.

Any idea what I may be doing wrong? I've been tempted to give up and just 'hardcoding' the children as part of the User table, but that limits the number and makes it less elegantly Cakey.

Here's the pertinent code I am using:

Models (have removed validation code etc for brevity)

class Instance extends AppModel {
public $belongsTo = array('Course');
public $hasMany = array('Booking');

}


class User extends AppModel {
public $hasMany = array('Booking','Kid');

}


class Kid extends AppModel {

public $belongsTo = array('User');
public $hasAndBelongsToMany = ('Booking');

}


class Booking extends AppModel {

public $belongsTo = array('Instance','User');
public $hasAndBelongsToMany = ('Kid');

}


InstanceController (am using this to process everything - all relevant models declared in $uses)

public function book($instanceID) 
    {

<--- CODE FOR CHECKING STUFF AND CREATING VARIABLES ETC-->
if ($this->request->is('post') and $status =='Confirmed') 
{
            $this->Booking->create();
            if ($this->Booking->saveAll($this->request->data)) 
            {
                $this->Session->setFlash(__('Your booking has been made. Thank you!'));
                return $this->redirect(array('controller'=>'instances','action' => 'view',$instanceID));
            }
            $this->Session->setFlash(__('There was an error - please check you have completed the form.'));
        }
    }



View (book.cp - edited for brevity)

echo $this->Form->create('Booking');
echo '<h3>Your Details</h3>';
echo $this->Form->input('User.firstname', array('label' => 'First name'));
echo $this->Form->input('User.surname', array('label' => 'Surname'));
echo $this->Form->input('User.username', array('label' => 'Email Address'));
echo $this->Form->input('User.phone', array('label' => 'Phone (mobile preferred)'));
if ($children == 1) {
echo "<h3>Your child's details</h3>";
echo $this->Form->input('Kid.name', array('label' => 'Child\'s Name'));
echo $this->Form->input('Kid.age', array('label' => 'Child\'s Age'));
} else {
echo "<h3>Your children's details</h3>";
$count = 1;
while ($count <= $children)
{
echo "<h4>Child " . $count . "</h4>";
echo $this->Form->input('Kid.'.$count.'.name', array('label' => 'Child\'s Name'));
echo $this->Form->input('Kid.'.$count.'.age', array('label' => 'Child\'s Age'));
echo "<hr>";
$count += 1;
}
}
echo $this->Form->end('Submit Booking'); 


Many thanks for looking!

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.