четверг, 31 марта 2011 г.

Zend_Validate_Db_RecordExists with doctrine

Validator Code:
<?php
class Validator_NoRecordExists extends Zend_Validate_Abstract
{
      private $_table;
      private $_field;

      const OK = '';

      protected $_messageTemplates = array(
          self::OK => "'%value%' allready in database"
      );

      public function __construct($table, $field) {
            if(is_null(Doctrine::getTable($table)))
                  return null;

            if(!Doctrine::getTable($table)->hasColumn($field))
                  return null;

            $this->_table = Doctrine::getTable($table);
            $this->_field = $field;
      }

      public function isValid($value)
      {
            $this->_setValue($value);

            $funcName = 'findBy' . $this->_field;

            if(count($this->_table->$funcName($value))>0) {
                  $this->_error();
                  return false;
            }

            return true;
      }
}

Use like:
$this->addElement('text', 'username', array(
      'validators' => array(
            array(
                  'validator' => new Validator_NoRecordExists('User','username')
            )
      )
));

 

четверг, 17 марта 2011 г.

Configure xDebug with Zend Studio or Eclipse IDE

system: gentoo | php: 5.3.3 | xdebug: 2.0.5

1. Install xdebug: emerge dev-php/xdebug-client -av
2. Edit php.ini: Include xdebug configuration file in php.ini.
    • create and put xdebug.ini file in directory: /etc/php/apache2-php5/ext/
    • create soft link: ln -s /etc/php/apache2-php5/ext/xdebug.ini /etc/php/apache2-php5/ext-active/
    •  xdebug.ini file content:  
    [xdebug]
    zend_extension=/usr/lib/php5/lib/extensions/no-debug-zts-20090626/xdebug.so
    xdebug.auto_trace="0"
    xdebug.trace_output_dir="/tmp"
    xdebug.trace_output_name="trace.%c"
    xdebug.trace_format="0"
    xdebug.trace_options="0"
    xdebug.collect_includes="1"
    xdebug.collect_params="0"
    xdebug.collect_return="0"
    xdebug.collect_vars="0"
    xdebug.default_enable="0"
    xdebug.extended_info="1"
    xdebug.manual_url="http://www.php.net"
    xdebug.max_nesting_level="100"
    xdebug.show_exception_trace="0"
    xdebug.show_local_vars="0"
    xdebug.show_mem_delta="0"
    xdebug.dump.COOKIE="NULL"
    xdebug.dump.ENV="NULL"
    xdebug.dump.FILES="NULL"
    xdebug.dump.GET="NULL"
    xdebug.dump.POST="NULL"
    xdebug.dump.REQUEST="NULL"
    xdebug.dump.SERVER="NULL"
    xdebug.dump.SESSION="NULL"
    xdebug.dump_globals="1"
    xdebug.dump_once="1"
    xdebug.dump_undefined="0"
    xdebug.profiler_enable="0"
    xdebug.profiler_output_dir="/tmp"
    xdebug.profiler_output_name="cachegrind.out.%p"
    xdebug.profiler_enable_trigger="0"
    xdebug.profiler_append="0"
    xdebug.profiler_aggregate="0"
    ;xdebug.remote_enable="0"
    xdebug.remote_enable=On
    xdebug.remote_handler="dbgp"
    xdebug.remote_host="localhost"
    xdebug.remote_mode="req"
    ;xdebug.remote_port="9000"
    xdebug.remote_port=9000
    xdebug.remote_autostart="0"
    ;xdebug.remote_log=""
    xdebug.remote_log="/var/www/xdebug.log"
    xdebug.idekey=""
    xdebug.var_display_max_data="512"
    xdebug.var_display_max_depth="2"
    xdebug.var_display_max_children="128"
    3. Check  phpinfo(). If all OK, you will see  "xdebug section"
    4. Configure Zend Studio.
    • Open your project in Zend Studio
    • In the main menu select Project->Properties
    • On the left side of the window select "PHP Debug" and then click on "Configure Workspace Settings"
    • On the "PHP Debugger" dropdown select Xdebug and click "Apply"
    • Click "Configure" to the right of Xdebug in the same window.
    • Select Xdebug and click "Configure".
    • On the "Accept remote session(JIT)" select "any" and click "OK". This is extremely important and this is where most people get stuck.
    5. Debug!!! :)

    very helpfull for me!

    пятница, 11 марта 2011 г.

    Custom Zend_Paginator_Adapter_Doctrine


    Source Code:

    <?php
    require_once ('Zend/Paginator/Adapter/Interface.php');
    /**
    * @author uommo
    * @link http://code.google.com/p/smart-framework/source/browse/trunk/library/SmartL/Zend/Paginator/Adapter/Doctrine.php?r=67
    */
    class Zend_Paginator_Adapter_Doctrine implements Zend_Paginator_Adapter_Interface
    {
    /**
    * Doctrine's selection for paginator.
    *
    * @var Doctrine_Query
    */
    private $_query;
    /**
    * Doctrine's selection for count of items.
    *
    * @var Doctrine_Query
    */
    private $_countQuery = null;
    /**
    * Hydration mode for doctrine select query.
    *
    * @var integer
    */
    private $_hydrationMode = null;

    /**
    * Constructs SmartL_Zend_Paginator_Doctrine
    *
    * @param Doctrine_Query|string $query
    * @param integer $hydratationMode Use constaints Doctrine::HYDRATE_*.
    * @param array[string]=>mixed $options Options may be:
    * 'countQuery'-custom query for count counting. Dql or Doctrine_Query instance.
    */
    public function __construct($query, $hydrationMode = null, $options = array())
    {
    if ( is_string($query) ) {
    $newQuery = new Doctrine_Query();
    $newQuery->parseDqlQuery($query);
    $query = $newQuery;
    }
    else if ( ! ($query instanceof Doctrine_Query) ) {
    require_once 'Zend/Paginator/Exception.php';
    throw new Zend_Paginator_Exception("Given query is not instance of Doctrine_Query");
    }
    $this->_query = $query;
    $this->_hydrationMode = $hydrationMode;

    //options
    if ( !empty($options['countQuery']) ) {
    if ( is_string($options['countQuery']) ) {
    $countQuery = new Doctrine_Query();
    $countQuery->parseDqlQuery($options['countQuery']);
    $options['countQuery'] = $countQuery;
    }
    else if ( ! ($options['countQuery'] instanceof Doctrine_Query) ) {
    require_once 'Zend/Paginator/Exception.php';
    throw new Zend_Paginator_Exception("Given count-query is not instance of Doctrine_Query");
    }
    $this->_countQuery = $options['countQuery'];
    $this->_countQuery->select('count(*) as count');
    }
    }

    /**
    * Returns query for count of items.
    *
    * @return Doctrine_Query
    */
    protected function getCountQuery()
    {
    if ( $this->_countQuery == null ) {
    $this->_countQuery = $this->_query->copy();
    $this->_countQuery->select('count(*) as count');

    $partsToBeRemoved = array('offset','limit','orderby');
    foreach( $partsToBeRemoved as $part ) {
    $this->_countQuery->removeDqlQueryPart($part);
    $this->_countQuery->removeSqlQueryPart($part);
    }
    }
    return $this->_countQuery;
    }

    /**
    * Implementation of method from Zend_Paginator_Adapter_Interface.
    *
    * @return integer
    */
    public function count()
    {
    $result = $this->getCountQuery()->execute(array(), Doctrine::HYDRATE_ARRAY);
    return $result[0]['count'];
    }

    /**
    * Implementation of method from Zend_Paginator_Adapter_Interface.
    *
    * @param integer $offset
    * @param integer $itemsPerPage
    * @return array[numeric|whatever]=>array|Doctrine_Record
    */
    public function getItems($offset, $itemsPerPage)
    {
    $this->_query->limit($itemsPerPage);
    $this->_query->offset($offset);
    $result = $this->_query->execute(array(), $this->_hydrationMode);
    return $result;
    }
    }




    PS. Big Thanks to author of this page: http://code.google.com/p/smart-framework/source/browse/trunk/library/SmartL/Zend/Paginator/Adapter/Doctrine.php?r=67



    среда, 2 марта 2011 г.

    Rename Network Interface using udev

    If you want rename a network interface's device name when using udev. This can be practical to do since the automatically assigned device name may actually change around a bit due to random variation in the initialisation process of them.
    For instance, eth0 can on one bootup refer to network interface A and eth1 to network interface B, but on another bootup can interface A be assigned eth1 and interface B be assigned eth0, which can cause problems if only one of them has a network cable attached and is configured to obtain an address over DHCP — the interface without the cable attached will then attempt to do that instead!
    udev Overview
    Unlike the traditional Linux system, where the device nodes in the /dev directory have been a static set of files, udev dynamically provides only the nodes for the devices actually present on a system. Although devfs provided a similar functionality, advocates for udev cited a number of reasons for preferring its implementation over devfs.
    udev supports persistent device naming, which does not depend on, for example, the order in which the devices are plugged into the system. The default udev setup provides persistent names for storage devices. Any hard disk is recognized by its unique filesystem id, the name of the disk and the physical location on the hardware it is connected to.
    udev executes entirely in user space, as opposed to devfs' kernel space. One consequence is that udev moved the naming policy out of the kernel and can run arbitrary programs to compose a name for the device from the device's properties, before the node is created.
    udev operation
    Udev is a generic kernel device manager. It runs as a daemon on a Linux system and listens to events the kernel sends out if a new device is initialized or a device is removed from the system. The system provides a set of rules that match against exported values of the event and properties of the discovered device. A matching rule will possibly name and create a device node and run configured programs to set-up and configure the device. Udev rules can match on properties like the kernel subsystem, the kernel device name, the physical location of the device, or properties like the device's serial number. Rules can also request information from external programs to name a device or specify a custom name that will always be the same, regardless of the order devices are discovered by the system.
    udev FAQ
    http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev-FAQ
    Comparison between udev and devfs
    http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev_vs_devfs
    Obtaining the MAC addresses from your Debian machine
    If you want to obtain MAC address of any linux machine you need to run the following command
    #ifconfig
    This will give you all the existing network interfaces in your machine
    example:-
    # ifconfig eth0
    eth0 Link encap:Ethernet HWaddr 00:AB:CD:12:34:56
    BROADCAST MULTICAST MTU:1500 Metric:1
    RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    TX packets:3 errors:0 dropped:0 overruns:0 carrier:3
    collisions:0 txqueuelen:1000
    RX bytes:0 (0.0 b) TX bytes:180 (180.0 b)
    Interrupt:185 Base address:0xc000
    In the above example HWaddr is your MAC address 00:AB:CD:12:34:56
    Using udev
    Create a new file in the udev rules directory, e.g. /etc/udev/rules.d/010_netinterfaces.rules
    In it specify the renaming in the following way for each interface on its own line
    KERNEL="oldnameprefix*", SYSFS{address}=="MACaddress", NAME="newname"
    where the oldnameprefix is typically eth. Note that in the MAC address, the hexadecimal digits should be in lowercase, otherwise udev fails to match them properly with the network interface.
    You have quite a bit of freedom in choosing the new name, We recommend to keep it short and without any spaces or weird characters though. You can e.g. specify a fixed eth0, eth1, eth2 for specific MAC addresses, or you can name them after their use, or anything really. Remember that some applications that poke on a low level may dislike them not being called in the normal fashion of eth0, eth1..etc
    Examples using udev
    Example: Three network interfaces being present on a computer, setting a fixed eth0, eth1 and eth2 as their names.
    KERNEL=="eth*", SYSFS{address}=="00:12:34:fe:dc:ba", NAME="eth0"
    KERNEL=="eth*", SYSFS{address}=="00:56:78:98:76:54", NAME="eth1"
    KERNEL=="eth*", SYSFS{address}=="00:90:ab:32:10:fe", NAME="eth2"Example: Three network interfaces (one Intel, one NVIDIA, and one 3Com) being present on a computer, naming them after the manufacturer of the interfaces.
    KERNEL=="eth*", SYSFS{address}=="00:12:34:fe:dc:ba", NAME="eth-intel"
    KERNEL=="eth*", SYSFS{address}=="00:56:78:98:76:54", NAME="eth-nv"
    KERNEL=="eth*", SYSFS{address}=="00:90:ab:32:10:fe", NAME="eth-3com"
    Updating network configuration
    If you named the interfaces in a different fashion as they were named before, the network configuration needs to be updated for the new interface device names to be used.
    Edit the /etc/network/interfaces file, and change all instances of the old names to the new names.
    E.g. if you previously used eth0 and have renamed it newname, you'd replace all instances of eth0 in that file with newname
    But if you just put a fixed eth0, eth1, ... as their names, you just need to make sure the one you want to have as the primary network interface is set to the one you want in the file.
    Example
    Having renamed the existing eth0, eth1, and eth2 to eth-intel, eth-nv and eth-3com, choosing to use the eth-intel one as the primary interface
    The /etc/network/interfaces file before changes
    # The primary network interface
    auto eth0
    iface eth0 inet dhcp
    # Currently unused network interfaces
    iface eth1 inet dhcp
    iface eth2 inet dhcpThe file after changes:
    # The primary network interface
    auto eth-intel
    iface eth-intel inet dhcp
    # Currently unused network interfaces
    iface eth-nv inet dhcp
    iface eth-3com inet dhcp
    Reboot and verify your configuration
    Reboot the computer and verify that the new network interface names are in use with e.g. ifconfig
    #ifconfig newname
    Where newname is the new interface name you specified. Repeat procedure for each one you renamed.
    *** In GENTOO udev, network configuration file is /etc/udev/rules.d/70-persistent-net.rules ***