如果数据库列为空,则过滤数组 symfony 2

Posted

技术标签:

【中文标题】如果数据库列为空,则过滤数组 symfony 2【英文标题】:filter array if database column empty symfony 2 【发布时间】:2016-06-01 03:38:06 【问题描述】:

有谁知道如何从 mysql 中过滤一列,这样如果该列为空,则不会显示任何数据。导致我出现问题的列是“him_paypal_item_name”,它使用数据库表中的值创建了一个“PayPal Buy now”按钮。链接过滤正常,但如果数据存在或不存在,paypal 和 skype 会呈现。我需要对它们进行一些过滤,以便仅在存在数据或值时显示。 我已经从扩展树枝上发布了下面的代码。

 <?php
// src/bamboo/tiseBundle/Twig/QRExtension.php
namespace bamboo\tiseBundle\Twig;

use Symfony\Component\HttpFoundation\Request;

class QRExtension extends \Twig_Extension

    /**
     * @var EntityManager
     */
    protected $em;

    public function __construct($em)
    
        $this->em = $em;
    

public function getFilters()

    return array(
        new \Twig_SimpleFilter('price', array($this, 'priceFilter')),
    );


public function getFunctions() 
    return array(
        'getFieldValue' => new \Twig_Function_Method($this, 'getFieldValue'),
        'getGallery' => new \Twig_Function_Method($this, 'getGallery'),
        'getType' => new \Twig_Function_Method($this, 'getType'),
    );


public function getFieldValue($serial, $categoryId) 

    return $this->em->getRepository('Webbamboo\tiseBundle\Entity\Him'.$categoryId)->findOneBySerialno($serial);


public function priceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')

    $price = number_format($number, $decimals, $decPoint, $thousandsSep);
    $price = '$'.$price;

    return $price;


public function getGallery($categoryData)

    $galleryKeys = array_values(array_filter(
        array_keys($categoryData),
        function($k) 
            if(strpos($k, 'him_gallery') === false)
            
                return false;
            
            return true;
        
    ));

    $youtubeKeys = array_values(array_filter(
        array_keys($categoryData),
        function($k) 
            if(strpos($k, 'him_youtubevideo') === false)
            
                return false;
            
            return true;
        
    ));
    $videoKeys = array('him_video');

    return array(
        'gallerykeys' => $galleryKeys,
        'youtubekeys' => $youtubeKeys,
        'videokeys' => $videoKeys,
    );


public function getType($fieldName, $categoryData, $categoryName, $serial, $useragent)

    if(strpos($useragent, 'iPhone') !== false)
    
        $mobileAction = 'callto:';
    
    elseif(strpos($useragent, 'android') !== false || strpos($useragent, 'silly') !== false)
    
        $mobileAction = 'wtai://wp/mc;';
    
    else
    
        $mobileAction = 'tel:';   
    
    $link = array(
        'him_email' => $this->getTypeProperties('mailto:', $categoryData[$fieldName], 'fa-envelope', 'Email', 'email'),
        'him_xbox' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-gamepad', 'xBox', 'xbox'),
        'him_stumbleupon' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-stumbleupon', 'StumbleUpon', 'stumbleupon'),
        'him_tripadvisor' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-tripadvisor', 'Tripadvisor', 'tripadvisor'),
        'him_instagram' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-instagram', 'Instagram', 'instragram'),
        'him_instragram' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-instagram', 'Instagram', 'instragram'),
        'him_onlinebooking' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-bookmark', 'Online Booking', 'onlinebook'),
        'him_website' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-home', 'Website', 'website'),
        'him_gform' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-users', 'Your Thoughts', 'gform'),
        'him_trustatrader' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-check-circle', 'Trust A Trader', 'trusta'),
        'him_twitter' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-twitter', 'Twitter', 'twitter'),
        'him_facebook' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-facebook', 'Facebook', 'facebook'),
        'him_youtube' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-youtube', 'YouTube', 'youtube'),
        'him_pinterest' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-pinterest-p', 'Pinterest', 'pinterest'),
        'him_google' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-google-plus', 'Google', 'google'),
        'him_linkedin' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-linkedin', 'LinkedIn', 'linkedin'),
        'him_trusted' => $this->getTypeProperties('', $categoryData[$fieldName], 'fa-check-circle', 'Trusted Trader', 'trusted'),
        'him_justgiving' => $this->getTypeProperties('', $categoryData[$fieldName], 'icon fa-custom-justgiving', 'Just Giving', 'justgiving'),
        'him_bebo' => $this->getTypeProperties('', $categoryData[$fieldName], '', 'Bebo', 'bebo'),
        'him_myspace' => $this->getTypeProperties('', $categoryData[$fieldName], 'icon fa-custom-myspace', 'MySpace', 'myspace'),
        'him_vehiclepx' => $this->getTypeProperties('', '#/pxvalue', 'fa-info', 'PX Value', 'vehiclepx', false),
        'him_price' => $this->getTypeProperties('', '#/vehicledetails', 'fa-list', 'View Details', $this->getRandomColor(), false),
        'him_vehicledetails' => $this->getTypeProperties('', '#/vehicledetails', 'fa-list', 'View Details', $this->getRandomColor(), false),
        'him_details' => $this->getTypeProperties('', '#/details', 'fa-list', 'View Details', $this->getRandomColor(), false),
        'him_address' => $this->getTypeProperties('', 'https://maps.google.co.uk/maps?q='.$categoryData[$fieldName], 'fa-map-marker', 'Address', 'address', true),
        'him_map' => $this->getTypeProperties('', 'https://maps.google.co.uk/maps?q='.$categoryData[$fieldName], 'fa-map-marker', 'Find us', 'address', true),
        'him_callback' => $this->getTypeProperties('mailto:', $categoryData['him_emailaddress'].'?subject=Request Callback&body=Please call me back regarding your advertised '.$categoryName.'( '.$serial.' ), My telephone number is --------', 'fa-phone', 'Request Callback', 'callback', true),
        'him_testdrive' => $this->getTypeProperties('mailto:', $categoryData['him_emailaddress'].'?subject=Request Test Drive&body=Please call me to arrange a test drive in your advertised vehicle '.$categoryName.'( '.$serial.' ), My telephone number is --------', 'fa-car', 'Request Callback', 'testdrive', true),
        'him_tel' => $this->getTypeProperties($mobileAction, $categoryData[$fieldName], 'fa-phone', 'Tel', 'tel', true),
        'him_mobile' => $this->getTypeProperties($mobileAction, $categoryData[$fieldName], 'fa-mobile', 'Mobile', 'mobile', true),
        'him_htel' => $this->getTypeProperties($mobileAction, $categoryData[$fieldName], 'fa-phone', 'Home Tel', 'htel', true),
        'him_calltobook' => $this->getTypeProperties($mobileAction, $categoryData[$fieldName], 'fa-phone-square', 'Call To Book', 'calltobook', true),
        'him_smsdonate' => $this->getTypeProperties('sms:', $categoryData['him_smsdonate'].'?body='.$categoryData['him_addword'], 'fa-mobile', 'Donate £'.$categoryData['him_amount'], 'smscall', true),
    );
    if(isset($link[$fieldName]) && !empty($categoryData[$fieldName]))
    
        if($fieldName == 'him_vehicledetails' && isset($categoryData['him_price']) && @!empty($categoryData['him_price']))
        
            return false;
        
        return $link[$fieldName] + array('type' => 'link');
    
    elseif($fieldName == 'him_paypal_item_name')
    
        //var_dump($fieldname);
        return array(
            'type' => 'paypal_pay',
            'business' => $categoryData['him_emailaddress'],
            'item_name' => $categoryData['him_paypal_item_name'],
            'item_number' => $categoryData['him_paypal_item_id'],
            'amount' => $categoryData['him_paypal_price'],
            'currency' => $categoryData['him_currency'],
            'tax_rate' => $categoryData['him_taxrate'],
            'shipping' => $categoryData['him_shipping'],
            'icon' => 'fa-cc-paypal',
            'color' => 'paypal'
        );   
    
    elseif($fieldName == 'him_donate')
    
        //var_dump($fieldname);
        return array(
            'type' => 'paypal_donate',
            'business' => $categoryData['him_donate'],
            'item_name' => $categoryData['him_logo'],
            'icon' => 'fa-cc-paypal',
            'color' => 'paypal'
        );   
    
    elseif($fieldName == 'him_skype')
       

        if(strpos($useragent, 'Android') !== false || strpos($useragent, 'silly') !== false)
        
            $skypeAction = 'skype:'.$categoryData['him_skypename'].'?call';
        
        else
        
            $skypeAction = '#';  
        
        return array(
            'type' => 'skype',
            'skypeName' => $categoryData['him_skype'],
            'action' => $skypeAction,
            'color' => $this->getRandomColor()
        );
    
    //dump($fieldName);
    return false;


private function getRandomColor()

    $colors = array('orange', 'lanora', 'alizarin', 'tile', 'emerald');  
    $rand_key = array_rand($colors, 1);
    return $colors[$rand_key];


private function getTypeProperties($action, $value, $icon, $label, $color, $blank = true)

    return array(
        'action' => $action,
        'value' => $value,
        'icon' => $icon,
        'label' => $label,
        'color' => $color,
        'blank' => $blank

    );   


public function getName()

    return 'tise_extension';


【问题讨论】:

elseif ($fieldName == 'him_paypal_item_name' &amp;&amp; !empty($categoryData[$fieldName]))这样的东西呢 非常感谢它有效!!!!现在你已经指明了方向,看起来很明显。 【参考方案1】:

您没有检查该值是否为空,您的 if 条件应如下所示:

elseif ($fieldName == 'him_paypal_item_name' && !empty($categoryData[$fieldName]))

您对其他字段进行了检查,但 him_paypal_item_name 中缺少该检查

【讨论】:

以上是关于如果数据库列为空,则过滤数组 symfony 2的主要内容,如果未能解决你的问题,请参考以下文章

SQL - 如果列为空,则列出所有数据

如果 Oracle OBIEE 中的列为空,则隐藏数据透视表中的列

如果另一列为空,则选择一列

如果为空,则过滤子文档数组,同时仍返回父数据

ArrayBlockingQueue源码分析

ArrayBlockingQueue源码分析