Anna FilinaDoctrine not saving ManyToMany (26.3.2015, 23:36 UTC)

Say you have a ManyToMany relationship, like Post <—> Tag.

// AcmeBundle/Entity/Post.php
/**
 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="posts", cascade={"persist","remove"})
 */
 protected $tags;
// AcmeBundle/Entity/Tag.php
/**
 * @ORM\ManyToMany(targetEntity="Post", mappedBy="tags", cascade={"persist","remove"})
 */
 protected $posts;

When you add tags to a Post and the save the Post, everything is great. But when you add posts to a Tag and then save the Tag, the posts are not saved. Why? Because Doctrine persists changes only on the owning side of a relation. The owning side is the one that has the inversedBy.

How to fix this? Simple. If you’re accessing the Entity directly (without a Symfony form), then there is only 1 step.

1. On the inverse side (Tag), you will need to edit the following methods: addPost and removePost.

public function addPost(Post $post)
{
    $this->posts[] = $post;
    $post->addTag($this);
    return $this;
}

public function removePost(Post $post)
{
    $this->posts->removeElement($post);
    $post->removeTag($this);
}

This will keep the owning side in sync, so that persisting either the Post or the Tag will save the association. At this point, you can go ahead and clean up the cascade in the Tag entity, as it’s no longer necessary.

@ORM\ManyToMany(targetEntity="Post", mappedBy="tags")

2. If you’re using a Symfony form with a collection field to associate the posts, you need to add an extra option in your form builder:

->add('posts', 'collection', array(
    'by_reference' => false,
    //...
))

This is only needed in the Tag form (the inverse side of the relationship). If you omit this option, it will default to true and will set posts by reference. It will go fetch the ArrayCollection of posts and manipulate it directly, therefore not calling addPost/removePost that you need to keep things in sync. Setting this option to false will force the form to call these methods.

Happy coding!

Link
Remi ColletPHP 7.0 as Software Collection (25.3.2015, 17:57 UTC)

RPM of upcoming major version of PHP 7.0, are available in remi repository for Fedora 20, 21, 22 and Enterprise Linux 6, 7 (RHEL, CentOS, ...) in a fresh new Software Collection (php70) allowing its installation beside the system version.

As I strongly believe in SCL potential to provide a simple way to allow installation of various versions simultaneously, and as I think it is useful to offer this feature to allow developers to test their applications, to allow sysadmin to prepare a migration or simply to use this version for some specific application, I decide to create this new SCL.

Installation :

yum --enablerepo=remi,remi-test install php70

emblem-important-2-24.pngTo be noticed:

  • the SCL is independant from the system, and doesn't alter it
  • this SCL is available in remi-test repository
  • installation is under the /opt/remi tree
  • the Apache module, php70-php, is available, but of course, only one mod_php can be used (so you have to disable or uninstall any other, the one provided by the default "php" package still have priority)
  • the FPM service (php70-php-fpm) is available, it listens on default port 9000, so you have to change the configuration if you want to use various FPM services simultaneously.
  • the php70 command give a simple access to this new version, however the scl command is still the recommended way (or the module command).
  • for now, the collection provides 7.0.0-dev, but alpha/beta version should be released soon
  • more PECL extensions will be progressively also available
  • only x86_64, no plan for other arch.

emblem-notice-24.pngAlso read other entries about SCL.

$ scl enable php70 'php -v'
PHP 7.0.0-dev (cli) (built: Mar 25 2015 14:40:01) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies

As always, your feedback is welcome, a SCL dedicated forum is open.

Link
SitePoint PHPTop 10 Z-Ray Features to Check Out (25.3.2015, 16:00 UTC)

This post was written by a developer who works on the Zend Server project. We’re pretty keen on editorial independence here at SitePoint, but occasionally we feature articles from people working directly on some of our favorite tools.

Necessity is the mother of invention goes the famous saying.

For PHP developers, there is no greater need than visibility. But developers today have a tough choice to make as they develop and debug their apps. Either use crude methods such as printing, debugging information, or storing it in a log file, or - use multiple debugging/profiling tools that are awkward and require a lot of work from the developer’s side.

Either way - this means time. Time spent accessing the PHP log. Time spent accessing SQL queries. Time spent finding that one bad function or PHP error causing a bottleneck or crashing your app.

This article introduces the top 10 features of Z-Ray - an innovative new technology from Zend that makes PHP development a whole lot quicker and easier by giving developers unprecedented insight into their code - and the visibility they need to develop top-notch apps.

About Zend Server

Zend Server is an integrated application platform for mobile and web PHP apps. It includes an advanced and secure PHP stack (which is fully compatible with php.net’s PHP and based on the same source code), with over 80 popular PHP extensions and libraries.

Zend Server includes tools that help you develop, debug, deploy and monitor your PHP apps, together with advanced features for optimizing performance, scalability and reliability.

Zend Server supports Apache, NGINX and IIS Web servers, and is delivered as native packages for all leading Linux distributions, Windows, MAC OS X and IBM i environments, including Cloud environments such as AWS.

Zend Server supports any PHP code, and provides deep insight into that code, including popular PHP applications and frameworks like WordPress, Magento, Drupal, Zend Framework, Symfony, and Laravel.

See this overview for more information on what Zend Server is and how to install it.

What’s Z-Ray?

Z-Ray is a Zend Server component that monitors requests made to the Web server, and then displays all the details of a page request, across all the PHP scripts involved in building the page, in a nifty toolbar at the bottom of your browser.

Z-Ray

Continue reading %Top 10 Z-Ray Features to Check Out%

Link
Derick RethansXdebug 2.3: Improvements to Debugging (25.3.2015, 09:12 UTC)

Xdebug 2.3: Improvements to Debugging

This is the fourth article in a series about new features in Xdebug 2.3, which was first released on February 22nd.

In this article we are looking at the improvements towards "remote" debugging.


The first improvement is the addition to view the values of user defined constants. In Xdebug this works by defining another context. Existing contexts are local variables, and super globals. In PhpStorm, they show up in the same "Variables" box:

Xdebug 2.3 defines a third context, "User defined constants". This new third context also shows up in PhpStorm's "Variables" box:

However, if you have lots of constants, that becomes annoying. Follow (or comment on) issue WI-21691 if you want to see this changed.

Other IDEs, such as Komodo, handle this by having a separate tab for constants:

It is actually clever enough to create a new tab for every new context that I come up with, as they haven't hard coded it.


Another new debugging feature is to be able to set an exception breakpoint on all exceptions, by using * as the exception name. Xdebug 2.3 also breaks on exception classes that are inherited from the ones you set a breakpoint on. With the following code:

<?php
class BaseException extends Exception {}
class ChildException extends BaseException {}

echo "start\n";

try
{
        echo "first try\n";
        throw new ChildException("No noes!");
}
catch ( ChildException $e )
{
        echo "eat it\n";
}

echo "end\n";
?>

And the following exception breakpoints configured in PhpStorm:

Xdebug will now interrupt the script when it hits line 11 because an exception breakpoint for both BaseException (the parent of the exception that we are throwing) and * are configured.


The third feature that I would like to highlight is related to the additions to debugging exceptions. Xdebug now includes the exception's error code in the reply to the IDE. However, it does not seem that PhpStorm allows me to see which exception it broke on, and what its message and error code are.

For now, you will have to do with the raw XML result (after formatting):

-> run -i 6
<?xml version="1.0" encoding="iso-8859-1"?>

        
                this should still break
        


I have created issue WI-26880 in their issue tracker as a feature request.


And the last thing that has changed with debugging in Xdebug 2.3 is that now Xdebug will try to log when you do not have sufficient permissions to create a socket. For example when you have SELinux turned on but not properly configured. The new message is:

E: No permission connecting to client. This could be SELinux related. :-(


Other parts in this series:

Link
Anthony FerraraThoughts On The Design Of APIs (24.3.2015, 15:00 UTC)
Developers as a whole suck at API design. We don't suck at making APIs. We don't suck at implementing them. We don't suck at using them (well, some more than others). But we do suck at designing them. In fact, we suck so much that we've made entire disciplines around trying to design better ones (BDD, DDD, TDD, etc). There are lots of reasons for this, but there are a few that I really want to focus on.

Read more »
Link
Derick RethansXdebug 2.3: Improvements to Debugging (24.3.2015, 09:12 UTC)

Xdebug 2.3: Improvements to Debugging

This is the fourth article in a series about new features in Xdebug 2.3, which was first released on February 22nd.

In this article we are looking at the improvements towards "remote" debugging. The first improvement is the addition to view the values of user defined constants.

  • issue 406, 495, 1066, 1084 (debugging section)


Other parts in this series:

Link
Cal EvansInterview with Samantha Quiñones (24.3.2015, 05:00 UTC) Link
Cal EvansInterview with Samantha Quiñones (24.3.2015, 05:00 UTC) Link
Paul M. JonesUsing Aura.Html with League\Plates (23.3.2015, 17:37 UTC)

Aura has its own native PHP template package, Aura.View, a direct descendant of Savant and Solar_View, as well as a cousin to Zend_View.

The v1 Aura.View package used to include a helper system. Once we realized that there was no reason to tie the helper system directly to the view system, we released the helpers as a standalone Aura.Html package. This means the helpers can be used in any PHP presentation code, framework-based or otherwise.

As an example, let’s try integrating the helpers with Plates, a relative newcomer in the native PHP templating world. Plates allows you to register functions with its template engine, which means we can pull individual helpers out of Aura.Html and drop them into Plates, like so:

<?php
$plates = new League\Plates\Engine('/path/to/templates');
$helper = (new Aura\Html\HelperLocatorFactory())->newInstance();

$plates->registerFunction('anchor',     $helper->get('anchor'));
$plates->registerFunction('anchorRaw',  $helper->get('anchorRaw'));
$plates->registerFunction('base',       $helper->get('base'));
$plates->registerFunction('form',       $helper->get('form'));
$plates->registerFunction('img',        $helper->get('img'));
$plates->registerFunction('input',      $helper->get('input'));
$plates->registerFunction('label',      $helper->get('label'));
$plates->registerFunction('links',      $helper->get('links'));
$plates->registerFunction('metas',      $helper->get('metas'));
$plates->registerFunction('ol',         $helper->get('ol'));
$plates->registerFunction('scripts',    $helper->get('scripts'));
$plates->registerFunction('styles',     $helper->get('styles'));
$plates->registerFunction('tag',        $helper->get('tag'));
$plates->registerFunction('title',      $helper->get('title'));
$plates->registerFunction('ul',         $helper->get('ul'));
?>

Now you can use the tag helpers and form-building helpers from Aura.Html in your Plates templates. For example, if your Plates template looks like this …

<html>
<head>
<?= $this->title('My Title'); ?>
</head>
<body>
<p>Try out <?= $this->anchor(
    'https://github.com/auraphp/Aura.Html',
    'Aura.Html'
); ?>
with <?= $this->anchor(
    'http://platesphp.com',
    'Plates'
); ?> !</p>
</body>
</html>

… it will render to:

<html>
<head>
    <title>My Title</title>
</head>
<body>
<p>Try out <a href="https://github.com/auraphp/Aura.Html">Aura.Html</a>
with <a href="http://platesphp.com">Plates</a> !</p>
</body>
</html>

Try out Aura.Html today, and see how much you like it with your output system of choice. (We’re partial to Aura.View for that task, but then, we would be.)

UPDATE: As usual, Hari KT is ahead of the curve with his post on this same topic from a year ago: http://harikt.com/blog/2014/05/13/extending-plates-with-aura-html-helpers/.

UPDATE 2: Someone asked how easy it is to use Aura.Html with Aura.View. It’s 3-lines-easy: see https://github.com/auraphp/Aura.View#custom-helper-managers.

Link
SitePoint PHPGetting Started with SQLite3 – Basic Commands (23.3.2015, 16:00 UTC)

SQLite is a C library that implements an SQL database engine. It is a Relational Database Management System (or RDBMS). Most of the SQL databases work with the client/server model. Take MySQL for an example. To enter and receive data from a MySQL database you need to send a request to the MySQL server, which on reception will provide you with the appropriate response. Contrary to MySQL, SQLite databases are operated directly from the disk. There is no need to create requests to the server.

SQLite370.svg (Custom)

Installation

We will operate on our database using the sqlite31 Command Line Interface(CLI). We will work under the Linux Platform. sqlite3 CLI for MAC OS and Windows are just the same, but for the sake of consistency, it’s recommended you install Homestead Improved - a lightweight VM runnable in 5 minutes that will keep your computer clean of extra unneeded software.

To install it on Homestead Improved (or any Debian based OS like Ubuntu), run the following:

[code language="bash"] sudo apt-get install sqlite3 libsqlite3-dev [/code]

This will install sqlite3. To install it on other platforms, follow their official instructions. After it is installed we can start the session. Open the Terminal/Command Line and enter the command sqlite3. You must see something of this kind :

CLI sqlite3

The second line tells you to enter .help for instructions. There is a prompt waiting for you to type a command. So go ahead and enter .help. This will give you a list of Meta Commands and their descriptions.

Continue reading %Getting Started with SQLite3 – Basic Commands%

Link
LinksRSS 0.92   RDF 1.
Atom Feed   100% Popoon
PHP5 powered   PEAR
ButtonsPlanet PHP   Planet PHP
Planet PHP