Kategorien
Doctrine ORM MySQL PHP Symfony2

GET random Object mit Doctrine

Doctrine selbst hat keine ORDER BY rand() Funktion implementiert. Aber es gibt eine Lösung. Man lässt sich den total count der rows ausgeben und nutzt die PHP interne rand() Funktion um FirstResult zu definieren.

    /**
     * @return mixed
     */
    public function getTotal()
    {
        return $this->getEntityManager()->createQueryBuilder()
            ->select('COUNT(p.id)')
            ->from('AppBundle:Product', 'p')
            ->getQuery()
            ->getSingleScalarResult();
    }

    /**
     * @return mixed
     */
    public function getRandom()
    {
        return $this->getEntityManager()->createQueryBuilder()
            ->select('p')
            ->from('AppBundle:Product', 'p')
            ->setFirstResult(rand(0, $this->getTotal()-1))
            ->setMaxResults(1)
            ->getQuery()
            ->getSingleResult();
    }
Kategorien
Frameworks Kohana Linux Nginx Server Shell

Kohana 2.3 NGINX htaccess like URL rewrite

Um Kohana 2.3 auf NGINX mit sauberen URLs zu konfigurieren geht folgende NGINX server config:

server {
        server_name example.com www.example.com;
        listen   80;

        root /srv/www/example.com/pages/;
        access_log /srv/www/example.com/logs/access.log;
        error_log /srv/www/example.com/logs/error.log;

        index index.php index.html index.htm;

        try_files $uri $uri/ @rewrite;

        location @rewrite {
                rewrite ^/(.*)$ /index.php/$1;
        }

        location ~ \.php {
                fastcgi_index index.php;
                fastcgi_pass 127.0.0.1:9000;

                include fastcgi_params;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param ENV "production";
        }
}
Kategorien
Debugging Doctrine ORM Frameworks MySQL Symfony2

SQL General error – 1553 Cannot drop index XYZ needed in a foreign key constraint

// show indexes of table
SHOW CREATE TABLE news;

// shows something like this
...
CONSTRAINT `FK_3A51546D12469DE2` FOREIGN KEY (`category_id`) REFERENCES `news_category` (`id`),
...

// drop index
ALTER TABLE news DROP FOREIGN KEY FK_3A51546D12469DE2;
Kategorien
CMS Systeme Frameworks PHP Sonata Symfony2

Symfony Sonata ACL – neue Einträge die nicht im Sonata erzeugt werden haben keine Rechte

Um in Symfony Sonata die ACL auch mit Einträgen zu nutzen, die durch ein Frontend oder im Hintergrund erzeugt werden, ohne den Sonatakontext, muss man den ACL Eintrag selbst erstellen. Dazu kann man einen EntityEventListener installieren und im postPersist Event folgendes tun:

    public function postPersist(LifecycleEventArgs $args)
    {
        $this->addAcl($args);
    }

    protected function addAcl(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        $adminSecurityHandler = $this->container->get('sonata.admin.security.handler');

        $adminPool  = $this->container->get('sonata.admin.pool');
        $admin      = $adminPool->getAdminByClass(get_class($entity));

        if (null !== $admin) {
            $objectIdentity = ObjectIdentity::fromDomainObject($entity);
            $acl = $adminSecurityHandler->getObjectAcl($objectIdentity);
            if (is_null($acl)) {
                $acl = $adminSecurityHandler->createAcl($objectIdentity);
            }
            $adminSecurityHandler->addObjectClassAces($acl, $adminSecurityHandler->buildSecurityInformation($admin));
            $adminSecurityHandler->updateAcl($acl);
        }
    }
Kategorien
Codeigniter Debugging Frameworks PHP

CodeIgniter Disallowed Key Characters FIX

Seit dem Google Analytics auch Cookies mit Sonderzeichen im Namen erstellt, springt die CodeIgniter Validierung darauf an und terminiert jeden Request mit der „Disallowed Key Characters“ Meldung.

Der Fix ist denkbar einfach. Man muss den Character (~) der Validierung mitteilen.
Im Beispiel unten in der PregMatch Definition „/^[~a-z0-9:_\/-]+$/i“.

# /system/libraries/Input.php (Zeile 217)

/**
 * Clean Keys
 *
 * This is a helper function. To prevent malicious users
 * from trying to exploit keys we make sure that keys are
 * only named with alpha-numeric text and a few other items.
 *
 * @access	private
 * @param	string
 * @return	string
 */
function _clean_input_keys($str)
{
    if ( ! preg_match("/^[~a-z0-9:_\/-]+$/i", $str))
    {
        exit('Disallowed Key Characters.');
    }

    return $str;
}
Kategorien
Doctrine ORM Frameworks MySQL PHP Symfony2

Symfony2 Doctrine Table Join ohne Relation

Wer in Doctrine Tabellen joinen möchte ohne das diese durch Entity Relationen definiert sind, kann folgendermaßen vorgehen:


    public function getInvoicesNotInElster()
    {
        $query = $this->em->createQueryBuilder()
            ->select('i')
            ->from('AcmeAppBundle:Invoice', 'i')
            ->leftJoin('AcmeAppBundle:Elster', 'e', 'WITH', 'e.number = i.number_origin')
            ->where('i.cancelled = 0')
            ->andWhere('e.number IS NULL')
            ->getQuery();

        return $query->getResult();
    }

Kategorien
CMS Systeme CSS Frameworks Layout & Styles Wordpress

FoundationPress – compass läd import pfade nicht

wenn ihr auch probleme habt die app.css in eurem FoundationPress starter theme für wordpress mit compass zu generieren, müsst ihr evtl. die internen pfade der @import dateien anpassen:

// in app.scss
@import "foundation";
replace with
@import "../bower_components/foundation/scss/foundation";

// in _settings.scss
@import "foundation/functions";
replace with
@import "../../bower_components/foundation/scss/foundation/functions";
Kategorien
Frameworks Kohana PHP Social Media Twitter

resolved – twitter api image upload

wer auch per twitter api posts senden will, hat evtl. auch schonmal mit dem gedanken gespielt, images gleich mit hochzuladen.
hier die syntax die tatsächlich funktioniert. api url ist in allen fällen https://api.twitter.com/1.1/

    public function updateStatusWithImage($title, $link, $desc, $img)
    {
        if(empty($img))
        {
            return $this->updateStatus($title, $link, $desc);
        }

        $status = $title.PHP_EOL.PHP_EOL.$link;
        $params = array( 
           'media[]' => '@'.$img,
           'status'   => $status
        );

        return $this->send('statuses/update_with_media.json', 'POST', $params);
    }

der vollständigkeit halber die ganze klasse (kohana style)

<?php defined('SYSPATH') OR die('No direct script access.');

class Gtwitter
{
    protected $api_url;
    protected $oauth_access_token;
    protected $oauth_access_token_secret;
    protected $consumer_key;
    protected $consumer_secret;

    public function __construct()
    {
        $twitter_config                     = Kohana::$config->load('gtwitter');
        $this->api_url                      = $twitter_config->get('api_url');
        $this->oauth_access_token           = $twitter_config->get('access_token');
        $this->oauth_access_token_secret    = $twitter_config->get('access_token_secret');
        $this->consumer_key                 = $twitter_config->get('consumer_key');
        $this->consumer_secret              = $twitter_config->get('consumer_secret');
    }

    public function getCredentials()
    {
        return $this->send('account/verify_credentials.json');
    }

    public function getTimeline()
    {
        return $this->send('statuses/user_timeline.json');
    }

    public function getFriends()
    {
        return $this->send('friends/list.json');
    }

    public function getFollowers()
    {
        return $this->send('followers/list.json');
    }

    public function updateStatus($title, $link, $desc)
    {
        $status = $title.PHP_EOL.PHP_EOL.$link;
        return $this->send('statuses/update.json', 'POST', array('status' => $status));
    }

    public function updateStatusWithImage($title, $link, $desc, $img)
    {
        if(empty($img))
        {
            return $this->updateStatus($title, $link, $desc);
        }

        $status = $title.PHP_EOL.PHP_EOL.$link;
        $params = array( 
           'media[]' => '@'.$img,
           'status'   => $status
        );

        return $this->send('statuses/update_with_media.json', 'POST', $params);
    }

    protected function buildBaseString($base_uri, $method, $params)
    {
        $retval = array();
        ksort($params);
        foreach($params as $key=>$value)
        {
            $retval[] = $key . '=' . rawurlencode($value);
        }
        return $method . '&' . rawurlencode($base_uri) . '&' . rawurlencode(implode('&', $retval));
    }

    protected function buildAuthorizationHeader($oauth)
    {
        $retval = 'Authorization: OAuth ';
        $values = array();
        foreach($oauth as $key=>$value)
        {
            $values[] = $key . '="' . rawurlencode($value) . '"';
        }
        $retval .= implode(', ', $values);
        return $retval;
    }

    protected function getOAuth()
    {
        return array(
            'oauth_consumer_key'        => $this->consumer_key,
            'oauth_nonce'               => time(),
            'oauth_signature_method'    => 'HMAC-SHA1',
            'oauth_token'               => $this->oauth_access_token,
            'oauth_timestamp'           => time(),
            'oauth_version'             => '1.0'
        );
    }

    protected function send($url, $method = 'GET', $params = array())
    {
        $url                        = $this->api_url.$url;
        if($method == 'GET' && !empty($params))
        {
            $url                    = $url.'?'.http_build_query($params);
        }
        $oauth                      = $this->getOAuth();
        $base_info                  = $this->buildBaseString($url, $method, $oauth);
        $composite_key              = rawurlencode($this->consumer_secret) . '&' . rawurlencode($this->oauth_access_token_secret);
        $oauth_signature            = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
        $oauth['oauth_signature']   = $oauth_signature;

        // do curl
        $header                         = array($this->buildAuthorizationHeader($oauth), 'Expect:');
        $opt                            = array();
        $opt[CURLOPT_HTTPHEADER]        = $header;
        $opt[CURLOPT_HEADER]            = false;
        $opt[CURLOPT_URL]               = $url;
        $opt[CURLOPT_RETURNTRANSFER]    = true;
        $opt[CURLOPT_FOLLOWLOCATION]    = true;
        $opt[CURLOPT_SSL_VERIFYPEER]    = false;
        if($method == 'POST')
        {
            $opt[CURLOPT_POST]          = true;
            $opt[CURLOPT_POSTFIELDS]    = $params;
        }

        $feed = curl_init();
        curl_setopt_array($feed, $opt);
        $json = curl_exec($feed);
        curl_close($feed);

        return json_decode($json, true);
    }

}
Kategorien
Debugging Frameworks Kohana PHP Server

howto get kohana 3.3.x or 3.2.x up and running when default route failes after fresh installation

copy the system/classes/kohana/request.php to your application/classes/kohana/request.php directory and open the file.
edit the file at around line 332 in the detect_uri method.


	public static function detect_uri()
	{

		...

		// cut out the initial base part to make sure the internal routing will get correct input
		$uri = str_replace(Kohana::$base_url.Kohana::$index_file, '', $uri);
		return $uri;
	}

Kategorien
Frameworks PHP Zend

zend certification knowledge base

if you are interested in doing a zend certification, have a look here

zend-php.appspot.com